【发布时间】:2011-08-20 03:56:36
【问题描述】:
这里是重现 ASP.NET MVC 3.0 路由的一个非常奇怪的问题的必要代码:
Global.asax.cs 中的路由注册:
routes.MapRoute("History", "Customer/History", new {controller = "User", action = "History", someParam = UrlParameter.Optional});
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
在这里,我们声明了一条通往用户历史记录的路线。但在 URL 中,我们想要“客户”而不是“用户”。另请注意参数someParam。控制器User 确实存在并且有动作History。
现在使用情况:
<a href="<%= Url.Action("History", "User") %>">History</a>
<a href="<%= Url.Action("History", "User", new { someParam="qqq" }) %>">History with param</a>
我在这里使用Url.Action() 而不是Html.ActionLink() 只是为了清楚起见。
这是结果 - 这部分视图是如何呈现的:
<a href="/Customer/History">History</a>
<a href="/User/History?someParam=qqq">History with param</a>
现在问题很清楚了 - 不带参数的 URL 已正确解析,而带参数的 URL 以“/User”而不是“/Customer”开头。
问题:
- 这是正常行为吗?如果是,为什么路由会这样工作?
-
有什么解决方法吗?我的意思是有什么办法可以得到最终结果:
<a href="/Customer/History">History</a> <a href="/Customer/History?someParam=qqq">History with param</a>
【问题讨论】:
标签: asp.net-mvc-3 asp.net-mvc-routing