how to remove the controller name from the url using rout in MVC [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

This question already has an answer here:

How to remove the controller name from the URL in MVC 5. I tried to add the route in the route.config

public class RouteConfig {     public static void RegisterRoutes(RouteCollection routes)     {         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");         routes.MapMvcAttributeRoutes();         routes.MapRoute(             name: "Default",             url: "{controller}/{action}/{id}",             defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }         );         routes.MapRoute(            name: "Special",            url: "action/{id}",            defaults: new { controller = "Home", action = "LandingIndex", id = UrlParameter.Optional }        );     } }

When I tried to a access the URL by http://localhost:24220/LandingIndex it will prompt 404 error. how to resolve this issue

回答1:

You could try inverting the routes definition by placing the more specialized route first. Also you probably didn't want to hardcode the action name as action but rather use the {action} placeholder:

public class RouteConfig {     public static void RegisterRoutes(RouteCollection routes)     {         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");         routes.MapMvcAttributeRoutes();          routes.MapRoute(             name: "Special",             url: "{action}",             defaults: new { controller = "Home", action = "LandingIndex" }         );          routes.MapRoute(             name: "Default",             url: "{controller}/{action}/{id}",             defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }         );     } }

Now when you navigate to /LandingIndex, the LanginIndex action on the Home controller should be invoked.

Also notice that I have removed the optional {id} placeholder from the first route. The reason for that is because if it were present, the routing engine would never use the second route. For example if you try to load /Home/Index it will look for a Home action on the Home controller and passing it Index as id. If you want to have this {id} token you will need to use a constraint to diambiguate with your second more general route definition. For example if your identifiers are always integers or follow some pattern, you could write a regex constraint that will match them.



回答2:

Thank you Darin Dimitrov little change in your replied code helps me to achieve the expected results

public class RouteConfig {     public static void RegisterRoutes(RouteCollection routes)     {         routes.IgnoreRoute("{resource}.axd/{*pathInfo}");         //routes.MapMvcAttributeRoutes();      routes.MapRoute(         name: "Special",         url: "{action}",         defaults: new { controller = "Home", action = "LandingIndex" }     );      routes.MapRoute(         name: "Default",         url: "{controller}/{action}/{id}",         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }     );    } }

just to remove the following line helps me

routes.MapMvcAttributeRoutes();



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