Implementation method of ASP. NET Core automatically generating lowercase dash route
- 2021-11-29 23:26:09
- OfStack
3.2 For Asp. Net Core2.2 Web API:
3.3 For Asp. Net Core > =3.0 MVC:
3.4 For Asp. Net Core > =3.0 Web API:
3.5 For Asp. Net Core > =3.0 Razor Pages:
3.6 For the above MVC project, the routing template needs to be adjusted a lot, but it can also be realized by implementing IControllerModelConvention.
By default, ASP. NET Core uses large hump routes such as http://localhost: 5000/HomeIndex. But if you want to use lowercase routes, and these routes are separated by dashes: http://localhost: 5000/home-index they are more common and 1.
Example .NET Common route
http://localhost:5000/User/ListPages
The desired effect
http://localhost:5000/user/list-pages
1. How to generate lowercase routes can be set as follows
services.ConfigureRouting(setupAction => {
setupAction.LowercaseUrls = true;
});
2. Generate a dashed lowercase route that can be set as follows
[Route("dashboard-settings")]
class DashboardSettings:Controller {
public IActionResult Index() {
// ...
}
}
It seems that using feature routing above can solve this problem. But I don't want to use it, because each action has to be set up manually, which is too cumbersome and error-prone.
The effect I want is to write an extension class in the program for configurable processing.
3. Solutions
The following supports Asp. Net Core Version > =2.2
To do this, first create an SlugifyParameterTransformer class that should look like this
public class SlugifyParameterTransformer : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
// Slugify value
return value == null ? null : Regex.Replace(value.ToString(), "([a-z])([A-Z])", "$1-$2").ToLower();
}
}
3.1 For Asp. Net Core2.2 MVC:
In StartUp, ConfiregeServices is configured like this
services.AddRouting(option =>
{
option.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
});
Routing is configured as follows:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller:slugify}/{action:slugify}/{id?}",
defaults: new { controller = "Home", action = "Index" });
});
3.2 For Asp. Net Core2.2 Web API:
In StartUp, ConfiregeServices is configured as follows
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer()));
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
3.3 For Asp. Net Core
>
=3.0 MVC:
In StartUp, ConfiregeServices is configured as follows
services.AddRouting(option =>
{
option.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
});
Routing is configured as follows:
app.UseEndpoints(endpoints =>
{
endpoints.MapAreaControllerRoute(
name: "AdminAreaRoute",
areaName: "Admin",
pattern: "admin/{controller:slugify=Dashboard}/{action:slugify=Index}/{id:slugify?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller:slugify}/{action:slugify}/{id:slugify?}",
defaults: new { controller = "Home", action = "Index" });
});
3.4 For Asp. Net Core
>
=3.0 Web API:
In StartUp, ConfiregeServices is configured as follows
services.AddControllers(options =>
{
options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer()));
});
3.5 For Asp. Net Core
>
=3.0 Razor Pages:
In StartUp, ConfiregeServices is configured as follows
services.ConfigureRouting(setupAction => {
setupAction.LowercaseUrls = true;
});
0
This causes the/Sys/UserList route to the/sys/user-list
3.6 For the above MVC project, the routing template needs to be adjusted a lot, but it can also be realized by implementing IControllerModelConvention.
services.ConfigureRouting(setupAction => {
setupAction.LowercaseUrls = true;
});
1
In StartUp, ConfiregeServices is configured as follows
services.ConfigureRouting(setupAction => {
setupAction.LowercaseUrls = true;
});
2
Translation: https://stackoverflow.com/questions/40334515/automatically-generate-lowercase-routes-in-asp-net-core
Translator: realyrare
Source: https://www.cnblogs.com/mhg215/
The above is ASP. NET Core automatic generation lowercase dash route implementation method details, more about ASP. NET Core generation lowercase dash route information please pay attention to other related articles on this site!