How to change URL Suffix of .net core?

霸气de小男生 提交于 2021-01-28 02:23:34

问题


How can I have the following route:

http://localhost:4413/abc/

And the following route:

http://localhost:4413/abc.html

both return the same controller method, using .NET Core MVC?


回答1:


It sounds like you want two suffixes to reach the same controller action.

  1. an empty suffix
  2. an .html suffix

Here is one way to do that in your Startup.Configure method. Edit its app.UseMvc to look like this:

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" });
});

Now, both routes will reach the same controller method.

http://localhost:4413/abc/      -->  HomeController.Abc()
http://localhost:4413/abc.html  -->  HomeController.Abc() 

See also: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1#special-case-for-dedicated-conventional-routes



来源:https://stackoverflow.com/questions/50895894/how-to-change-url-suffix-of-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!