我正在使用 MVC 5 和 Angular 2 开发单页应用程序。
我的要求是我需要为同一 Controller /操作方法设置多个路由。
如果用户输入 url http://localhost:xxxx/home/index ,它将针对“home” Controller 的“index”操作方法。
我也 希望“http://localhost:xxxx/products”和“http://localhost:xxxx/categories”指向“home” Controller 的“index”操作方法。
注意:“产品”和“类别”都不是 Controller 或操作方法。
最佳答案
添加以下 2 条路线即可完成任务。
routes.MapRoute(
name: "categories",
url: "categories",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "products",
url: "products",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
关于asp.net-mvc - 如何在 MVC 5 中将多个路由设置为同一操作方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41909047/