ASP.NET MVC : Empty ActionLinks appearing

前端 未结 2 918
执念已碎
执念已碎 2021-01-15 03:29

I\'m using a default route, so that I don\'t need to specify the controller.

routes.MapRoute(
    \"Default\", 
    \"{action}/{id}\", 
     new { controlle         


        
2条回答
  •  耶瑟儿~
    2021-01-15 03:38

    Found the answer! There's a bug in MVC3 when using two consecutive optional UrlParameters, as detailed by Phil Haack, here routing-regression-with-two-consecutive-optional-url-parameters

    You need to first declare a version of the route with only one optional parameter. So

    routes.MapRoute(
        "Default", // Route name
        "{action}/{id}", // URL with ONE parameter
        new { controller = "Home", action = "Index", 
        id = UrlParameter.Optional } // Parameter defaults
    );
    
    routes.MapRoute(
        "Default_with_page", // Route name
        "{action}/{id}/{page}", // URL with TWO parameters
        new { controller = "Home", action = "Index", 
        id = UrlParameter.Optional, page = UrlParameter.Optional } 
        // Parameter defaults
    );
    

    Seems really obvious now. If I'd actually included all the details I'm sure Serghei or someone else would have seen the problem, so thanks for all the help guys!

提交回复
热议问题