如何更改.net core 的URL 后缀?

How to change URL Suffix of .net core?

怎样才能有如下路由:

http://localhost:4413/abc/

以及以下路线:

http://localhost:4413/abc.html

两者return相同的控制器方法,使用.NET Core MVC?

听起来您希望两个后缀达到相同的控制器操作。

  1. 空后缀
  2. 一个.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