After a change to the default routing map my actionlinks stopped working

我只是一个虾纸丫 提交于 2019-12-12 01:02:43

问题


I made a change to my routing map which is following now:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}/{title}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, title = UrlParameter.Optional }
            );

And this is the actionlinks that are not working anymore:

@Html.ActionLink("Category", "CategoryList", "Category")

When I click on this actionlink nothing happens the url stays the same http://localhost:62394/ as the page reloads. its so weird

and when I check the html it looks like this:

<a href="">Category</a>

Any kind of help or tips is appreciate alot!

note: If I remove title from the routing it works...


回答1:


It's because you've got 2 optional parameters so the routing engine doesn't know which one to map the third parameter to. It feels like title is going to be used for a specific route rather than a generic one. If that's the case, why not create a specific route for it and remove it from the generic fallback route?

Something like this:

routes.MapRoute(
            name: "Title",
            url: "Category-List/{title}",
            defaults: new { controller = "Category", action = "CategoryList", title = UrlParameter.Optional }
        );
routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );


来源:https://stackoverflow.com/questions/16625337/after-a-change-to-the-default-routing-map-my-actionlinks-stopped-working

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