MVC3 路由 - 相互构建的路由

发布于 2024-12-07 17:02:28 字数 559 浏览 0 评论 0原文

我的项目中有一个 AREA 设置。我需要使该地区的路线渐进,这意味着路线将相互构建。

我将其视为类似链接列表的东西。列表中的每个节点都会有一个对父节点的引用。当在列表中从左到右移动时,它会构建,从右到左移动时,它会删除。

在该地区,我有一些有联系的公司,还有子公司。

例如,我的公司具有以下内容:

 /Companies/list
  /Company/{Id}
  /Company/{id}/add
  /Company/{id}/edit
  /Company/{id}/delete

对于联系人部分,我需要创建以下路由:

 /Company/{id}/contacts/list
 /Company/{id}/contact/{id}/add
 /Company/{id}/contact/{id}/edit
 /Company/{id}/contact/{id}/delete

如何确保 /Company/{id} 始终位于路由的“联系人”和“子公司”部分中?

我希望我已经把我的问题说清楚了。

I have an AREA setup in my project. I need to make the routes for the area progressive, meaning that the route will build on each other.

I'm looking at this as something like a link list. Each node in the list will have a reference to a parent. As move from left to right in the list it builds, and from right to left it removes.

In the area, I have companies and that have contacts, and child companies.

For example, I have companies that would have the following:

 /Companies/list
  /Company/{Id}
  /Company/{id}/add
  /Company/{id}/edit
  /Company/{id}/delete

For the contact section I need to create the following routes:

 /Company/{id}/contacts/list
 /Company/{id}/contact/{id}/add
 /Company/{id}/contact/{id}/edit
 /Company/{id}/contact/{id}/delete

How do I make sure that /Company/{id} is always in the Contact and Child Company sections of the route?

I hope that I have made my question clear.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别忘他 2024-12-14 17:02:28

主观概括(持保留态度):

首先,您使用 Company(单数)表示公司,然后使用 contact(复数)表示联系人。从结构的角度来看,这并没有什么问题,但是如果您与多元化保持一致,您的用户会感谢您。在这两种情况下我都会使用复数,但这只是我的偏好......它看起来更像英语。

您还可以使用小写字母表示联系人,使用大写字母表示公司。看起来不专业。

接下来令人困惑的是您使用了两个 {id} 参数,一个用于公司,一个用于联系人。我认为这些分别是公司和联系人的 ID。但我很困惑,但作为人类,我能够像计算机那样推断出上下文。因此,您最好在路线中指定参数。即:

/Companies/{CompanyId}/Contacts/{ContactId}/[action]

用示例回答您的问题:

我感觉您没有正确理解路线。如果你这样做,你的问题会更具体。

您的路线参数可以来自多个来源,具体取决于请求路线的方式。

您可以将其硬编码到链接中。或者,更有用的是,您的路由注册旨在捕获映射到您的操作签名的请求。

例如,我有一个包含导师、学生、课程和步骤的电子学习应用程序(即,步骤就像课程的各个部分,学生逐步完成课程)

路线注册看起来像:

路线或区域注册:

context.MapRoute(
    "StepDisplay",
    "Course/{CourseId}/Step/{StepOrder}/Pupil/{PupilName}/{TutorName}",
    new { controller = "Course", action = "Display", TutorName = UrlParameter.Optional },
new[] { "ES.eLearningFE.Areas.Courses.Controllers" }
);

此路由将捕获来自以下 ActionLink 的请求:

视图中的 ActionLink:

@Html.ActionLink(@StepTitle, MVC.Courses.Course.Actions.Display(Model.CourseId, step.StepOrder, Model.Pupil.UserName, tutorName))

现在,我只需向您显示 Display 操作的签名:

CoursesController: >

public virtual ActionResult Display(int CourseId, int StepOrder, string PupilName, string TutorName)

有几件事这里要注意的是:

  1. 我可以通过为用户提供一个可单击的链接来调用此特定路线。

  2. 我使用 Html.ActionLink 帮助程序构建此链接

  3. 我使用了 David Ebbo 的 t4mvc nuget 包,以便我可以指定我正在调用的操作及其参数。我的意思是使用以下方法指定 Html.ActionLink 帮助器的 ActionResult 参数:

    MVC.Courses.Course.Actions.Display(Model.CourseId, step.StepOrder, Model.Pupil.UserName, coachName)

如果您考虑一下,什么路由do 是将请求的 url 转换为操作,因此我的路由的参数要么是控制器名称,操作名称,要么是操作签名中的参数名称。

您现在可以明白为什么用相同的名称命名两个不同的路由参数了
name 是个坏主意(主要是因为它不起作用)。

因此,请查看您的动作签名,并设计您的路线和动作链接,以便将所有内容结合在一起。

MVC 并不是靠魔法来工作的! (尽管它使用命名约定的方式可能会让你相信它)

Subjective Generalities (take with a pinch of salt):

First off, you are using Company (singular) for companies, but then you are using contacts (plural) for the contacts. There is nothing wrong with this, from a structural point of view, but your users will thank you if you are consistent with your pluralizations. I would use the plural in both cases, but that is just my preference... it looks more like English.

You also use lower case for contacts, but upper case for Company. Doesn't look professional.

The next thing that is confusing is that you are using two {id} parameters, one for companies, one for contacts. I presume these are the ids for Company and Contacts respectively. But I am confused, but being human, I am able to deduce context unlike a computer. So you would be better of specifying the parameters in your routes. Ie:

/Companies/{CompanyId}/Contacts/{ContactId}/[action]

Answering your Question with an Example:

I get the feel you don't understand routes properly. If you did, your question would be more specific.

Your route parameters can come from a number of sources, depending on how the route is requested.

You could hard code it into a link. Or, more usefully, your route registration would be designed to catch requests that map to your Action signatures.

For example, I have an eLearning app with tutors, pupils, courses and steps (ie, the steps are like sections of a course, the pupil advances through the course step by step)

The route registration looks something like:

Route or Area Registration:

context.MapRoute(
    "StepDisplay",
    "Course/{CourseId}/Step/{StepOrder}/Pupil/{PupilName}/{TutorName}",
    new { controller = "Course", action = "Display", TutorName = UrlParameter.Optional },
new[] { "ES.eLearningFE.Areas.Courses.Controllers" }
);

This route will catch a request from the following ActionLink:

ActionLink in View:

@Html.ActionLink(@StepTitle, MVC.Courses.Course.Actions.Display(Model.CourseId, step.StepOrder, Model.Pupil.UserName, tutorName))

Now, I just need to show you the Display action's signature:

CoursesController:

public virtual ActionResult Display(int CourseId, int StepOrder, string PupilName, string TutorName)

There are a few things to note here:

  1. That I am able to call this specific route by giving the user a link to click on.

  2. I construct this link using the Html.ActionLink helper

  3. I have used David Ebbo's t4mvc nuget package so that I can specify the action I am calling and its parameters. By which I mean specifying the ActionResult parameter of the Html.ActionLink helper using:

    MVC.Courses.Course.Actions.Display(Model.CourseId, step.StepOrder, Model.Pupil.UserName, tutorName)

If you think about it, what routes do is translate the url of a request into an action, so the parameters of my route are either the controller name, the action name or else they are the names of parameters in the action signature.

You can see now why naming two distinct route parameters with the same
name is such a bad idea (largely because it won't work).

So, look at your action signatures, and design your routes and your action links so that the everything marries up together.

MVC doesn't work by magic!! (Although the way it uses name conventions might lead you to believe it)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文