ASP.NET MVC 中如何选择生成传出 URL 的路由?
再会!
似乎正在发生一些代码魔法
我正在使用 ASP.NET MVC 2 和 T4MVC,当我将其添加到路由表时,
routes.MapRoute(
"Login",
"login/",
MVC.Profile.Login()
);
:当我在视图中编写类似的内容以生成传出时,框架如何知道我希望应用此规则URL:
<%: Url.Action(MVC.Profile.Login() %>
如果我对同一控制器/操作对有多个不同的规则(具有不同的参数)怎么办?会选择哪一位呢?有没有关于这种行为的很好的描述?
提前致谢!
Good day!
I'm using ASP.NET MVC 2 and T4MVC and it seems some code magic is happening
When I add this to routes table:
routes.MapRoute(
"Login",
"login/",
MVC.Profile.Login()
);
How does framework know that I want this rule to apply when I write something like this in the view to generate outgoing URL:
<%: Url.Action(MVC.Profile.Login() %>
What if I have multiple different rules (with different params) for the same controller/action pair? Which one will be chosen? Is there anywhere a good description of this behavior?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议帮助您理解这项工作是如何将 T4MVC 的魔力与 MVC 本身在幕后所做的事情区分开来。
当您使用 T4MVC 编写此内容时:
相当于使用直接 MVC 编写此内容:
并且在视图中:
与 T4MVC相同
,为您提供强类型/智能感知的好处,但最终它的作用与直接 MVC 相同。
希望这有助于澄清一些事情:)
What I would suggest to help you understand how this work is to separate the magic that T4MVC does from what MVC itself does under the cover.
When you write this with T4MVC:
It's equivalent to writing this with straight MVC:
And in the view:
Is the same as
T4MVC gives you the benefit of strong typing/intellisense, but in the end what it does is the same as with straight MVC.
Hopefully this helps clear things up a bit :)
它按照您定义的顺序匹配路由模式。这就是为什么你将默认模式作为最后一个模式。一旦找到匹配的模式,它就会停止寻找。
编辑
参数在路由匹配过程中被忽略。选择控制器方法后,mvc 使用模型绑定将参数分配给方法变量。
如果您可以解释您想要使用什么类型的 url 结构,我们可能可以为您提供更多帮助。
It matches the route patterns in the order you define them. Thats why you have the default pattern as the last one. As soon as it finds a matching pattern, it stops looking.
Edit
Parameters are ignored during route matching. Once a controller method has been selected, mvc uses model binding to assign the parameters to the method variables.
If you can explain what type of url structure you are looking to use, we can probably help you more.
您的示例不是有效的 MVC,您通常会传递控制器名称、操作和任何其他参数,然后路由引擎将使用所有这些信息来确定要使用哪个路由,您定义的路由越多,它可能会提供的信息就越多需要确定您想要匹配的那个
Your example is not valid MVC, you would normally pass the controller name, action and any other parameters, then the routing engine would use all that information to determine which route to use, the more routes you have defined, the more information it will probably require to determine the one YOU want to match