开发者

asp.net mvc3 RouteLink

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", "Catego开发者_如何学运维ry", new { cat = "Women", subcat = "" })%>

This is how it renders

<a href="">Women's</a>


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 ]


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜