如何更改.net核心的URL后缀?

时间:2018-06-17 10:46:29

标签: asp.net-core .net-core asp.net-core-2.0 asp.net-core-routing

我如何拥有以下路线:

http://localhost:4413/abc/

以下路线:

http://localhost:4413/abc.html

使用.NET Core MVC返回相同的控制器方法?

1 个答案:

答案 0 :(得分:2)

听起来你想要两个后缀来达到相同的控制器动作。

  1. 空后缀
  2. .html后缀
  3. 以下是在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