问题
I want to do make several map route in MVC that both like be same.
localhost:1010/abcd/home/index
localhost:1010/home/index/abcd
id=abcd controller=home action=index
I used bellow code but it doesn't work
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ShoppingManagment",
"{id}/{controller}/{action}",
new { controller = "ShoppingManagment",
action = "ShoppingManagment", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index", id = UrlParameter.Optional }
);
}
回答1:
It will not work because both routes have the same format.
So the MVC Routing Engine cannot differentiate between both the url patterns.
Try writing the Controller directly into the url pattern.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"ShoppingManagment",
"{id}/ShoppingManagment/{action}",
new { controller="ShoppingManagment", action = "ShoppingManagment", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index", id = UrlParameter.Optional }
);
}
来源:https://stackoverflow.com/questions/16139743/registering-multiple-routes-in-mvc-asp-net