MVC3路由 - 路由一个建立在相互 [英] MVC3 Routing - Routes that builds on each other

查看:183
本文介绍了MVC3路由 - 路由一个建立在相互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中的区域设置。我需要的区域路线进步,意味着路由将建立在对方。

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.

推荐答案

首先,您使用的企业公司(单数),但你正在使用的联系方式联系方式(复数)。没有什么不对的,从结构上来看,但你的用户会感谢你,如果你与你的复数形式是一致的。我会用复数在这两种情况下,但是这只是我的preference ...它看起来更像是英语。

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.

这是混淆了接下来的事情是,你使用两个{ID}参数,一个公司,一个联系人。我presume这些分别公司和联系人的ID。但我很困惑,但作为人,我能够演绎出不同环境的计算机。所以,你会在你的路由指定参数的更好。即:

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.

您辛苦code它可以变成一个链接。或者,更有效,你的路线报名将设计为捕获映射到你的行动的签名请求。

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:

路线或地区注册:

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的请求:

This route will catch a request from the following ActionLink:

ActionLink的在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. 这是我能够通过给用户一个链接,点击调用这个特定的路线。

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

我构建使用Html.ActionLink帮手此链接

I construct this link using the Html.ActionLink helper

我用大卫Ebbo的t4mvc的NuGet包,这样我可以指定我打电话的动作和它的参数。我的意思是使用指定Html.ActionLink帮手的ActionResult参数:

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)

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

如果你仔细想想,做的是什么路线转化成行动的请求的URL,所以我的路线的参数或者是控制器名称,操作名称,否则它们的参数在操作签名的名字

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不会魔法工作! (虽然它的方式使用命名约定可能导致你相信吧)

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

这篇关于MVC3路由 - 路由一个建立在相互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆