ActionLink和路由 [英] ActionLink and Routing

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

问题描述

您好。我一直关注Adam Freeman的Pro ASP.NET MVC 4书,我对ActionLink的路由匹配行为感到困惑。以下是我的RegisterRoute和ActionLink方法:



Hello. I have been following Pro ASP.NET MVC 4 book by Adam Freeman, and i'm confused about the ActionLink's route matching behaviour. Below are my RegisterRoute and ActionLink method :

public static void RegisterRoutes(RouteCollection routes) {
routes.MapRoute("NewRoute", "App/Do{action}",
new { controller = "Home" });
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index",
id = UrlParameter.Optional });
}







@Html.ActionLink("This is an outgoing URL", "CustomVariable")





上面的ActionLink指向



The ActionLink above points to

<a href="/App/DoCustomVariable">This is an outgoing URL</a>





我发现这令人费解。根据路由规则,它不应与



I find that puzzling. According to the routing rules, it shouldn't match with

/App/DoCustomVariable

匹配,因为它缺少静态片段。它应该指向/ Home。



提前致谢。

since it lacks the static fragment for it. It should point to /Home.

Thanks in advance.

推荐答案

按顺序查找匹配并返回第一个有效匹配。因此,虽然它匹配您的第二条路线,但它也匹配第一条路线,因此这是使用的路线。将最常用的路由放在最后,将最具体的路由放在开头。
It looks for a match in order and returns the first valid one. So while it matches your second route, it also matches the first so that is the route used. Put your most generic routes at the end and the most specific ones at the start.


这篇关于ActionLink和路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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