asp.net mvc3 RouteLink

六眼飞鱼酱① 提交于 2019-12-12 10:16:09

问题


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>

回答1:


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 ]




回答2:


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 and page cannot be optional. You need to supply their values:

<%: Html.RouteLink("Women's", "Category", new { 
    cat = "Women", 
    subcat = "somesubcat", 
    page = "123"  
})%>

In ASP.NET MVC 3 this rule was enforced.

Consider the following urls:

category/1
category/1/2/
category/1/2/3
category/1/2/3/4

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.



来源:https://stackoverflow.com/questions/4788267/asp-net-mvc3-routelink

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!