我在ASP.NET Core2.1MVC中有一个map路由:
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
HomeController
具有操作
Index
显示默认站点pageProjects
显示projectsProject
列表显示有关特定项目的详细信息(以其id作为输入)项目中还有其他控制器。
我想以如下方式更新路由,即urls:
在url中没有Home
的情况下映射
Home
控制器动作。我不想列出路由定义中的所有操作,规则应该是通用的。HomeController.Index
/
HomeController.Projects
projects
)HomeController.Project(id: 4)
/Projects
(或在小写project/4
)中更好)/
HomeController.Projects
=> => /Projects
(在小写project/4
)中更好
Controller/Action
.实现这一目标的推荐方法是什么?有可能吗?
发布于 2018-07-25 20:48:49
routes.MapRoute(
name: "other",
template: "{controller}/{action}/{id?}");
routes.MapRoute(
name: "home",
template: "{action=Index}/{id?}",
defaults: new { controller = "Home" });
发布于 2018-07-25 20:45:08
要定义一个默认控制器而不在url中指定它,只需将它添加到‘default’参数中即可。
要将这种路由组合到一个规则中,使用相同的基本url会更简洁,即使用/Products
和/Products/4
。然后你可以使用:
routes.MapRoute(
name: "products",
template: "Products/{id?}",
defaults: new { controller = "Home", Action = "Products" });
在你的结构中,类似这样的东西应该是有效的:
routes.MapRoute(
name: "products",
template: "{Action:regex(^(Products|Product)$)/{id?}",
defaults: new { controller = "Home" });
要处理不符合此规则的urls,请在products- /
之后添加原始默认路由。
routes.MapRoute(
name: "products",
template: "{Action:regex(^(Products|Product)$)/{id?}",
defaults: new { controller = "Home" });
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
https://stackoverflow.com/questions/51518472
复制相似问题