问题
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.
- an empty suffix
- 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