asp.net mvc3 RouteLink
我刚刚将我的 mvc2 应用程序升级到 mvc3。并且路线链接停止工作。有什么线索吗?
全局
routes.MapRoute(
"Category",
"category/{cat}/{subcat}/{page}/{viewall}",
new
{
controller = "Category",
action = "Index",
cat = UrlParameter.Optional,
subcat = UrlParameter.Optional,
page = UrlParameter.Optional,
viewall = UrlParameter.Optional
}
);
视图
<%: Html.RouteLink("Women's", "Category", new { cat = "Women", subcat = "" })%>
这是它的渲染方式
<a href="">Women's</a>
I have just upgraded my mvc2 app to mvc3. And the routelink stopped working. any clue??
Global
routes.MapRoute(
"Category",
"category/{cat}/{subcat}/{page}/{viewall}",
new
{
controller = "Category",
action = "Index",
cat = UrlParameter.Optional,
subcat = UrlParameter.Optional,
page = UrlParameter.Optional,
viewall = UrlParameter.Optional
}
);
View
<%: Html.RouteLink("Women's", "Category", new { cat = "Women", subcat = "" })%>
This is how it renders
<a href="">Women's</a>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个回归错误,正如 Phil Haack 所解释的 [ http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx]
It's a regression bug, as explained by Phil Haack [ http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx ]
这很正常。您只能有一个可选参数,并且该参数应该是路由定义中的最后一个参数。所以
cat
、subcat
和page
不能是可选的。您需要提供它们的值:在 ASP.NET MVC 3 中强制执行此规则。
考虑以下 url:
只有最后两个 url 是可能的,因为这是路由参数可以毫无歧义地映射到其相应值的唯一情况。
That's normal. You can have only one optional parameter and this parameter should be the last one in your route definition. So
cat
,subcat
andpage
cannot be optional. You need to supply their values:In ASP.NET MVC 3 this rule was enforced.
Consider the following urls:
Only the last two urls are possible because it's the only case where the route parameters could be mapped to their corresponding values without ambiguity.