【问题标题】:How to change URL Suffix of .net core?如何更改 .net core 的 URL 后缀?
【发布时间】:2018-06-17 10:46:29
【问题描述】:
我怎样才能有以下路线:
http://localhost:4413/abc/
还有以下路线:
http://localhost:4413/abc.html
两者都返回相同的控制器方法,使用 .NET Core MVC?
【问题讨论】:
标签:
asp.net-core
.net-core
asp.net-core-2.0
asp.net-core-routing
【解决方案1】:
听起来您希望两个后缀达到相同的控制器操作。
- 空后缀
-
.html 后缀
这是在您的 Startup.Configure 方法中执行此操作的一种方法。将其app.UseMvc 编辑为如下所示:
app.UseMvc(routes =>
{
// this is the default route that is already present
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
// map the empty suffix
routes.MapRoute(
name: "home",
template: "{action}",
defaults: new { Controller = "Home" });
// map the .html suffix
routes.MapRoute(
name: "home.html",
template: "{action}.html",
defaults: new { Controller = "Home" });
});
现在,两条路由都将到达同一个控制器方法。
http://localhost:4413/abc/ --> HomeController.Abc()
http://localhost:4413/abc.html --> HomeController.Abc()
另见:https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1#special-case-for-dedicated-conventional-routes