默认环境下,ASP.NET Core利用如 :5000/HomeIndex 类的大驼峰路由。可是假如想利用小写的路由,而且这些路由用破折号脱离::5000/home-index它们较量常见且一致。
举例.NET常见路由 :5000/User/ListPages 想要的结果 :5000/user/list-pages
1、如何生成小写的路由可以这样配置services.ConfigureRouting(setupAction => { setupAction.LowercaseUrls = true; });
2、生成带破折号而且小写的路由可以这样配置[Route("dashboard-settings")] class DashboardSettings:Controller { public IActionResult Index() { // ... } }
好像上面利用特性路由可以办理这个问题。可是我不想利用,因为每个action都要手动去配置,太繁琐也很容易堕落。
我想要的结果是在措施中写个扩展类做到可设置处理惩罚。
3、办理方案以下支持Asp.Net Core Version>=2.2
要做到这一点,首先建设SlugifyParameterTransformer类应该如下所示
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 对付Asp.Net Core2.2 MVC:在StartUp中ConfiregeServices这样设置
services.AddRouting(option => { option.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer); });
路由如下设置:
app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller:slugify}/{action:slugify}/{id?}", defaults: new { controller = "Home", action = "Index" }); });
3.2 对付Asp.Net Core2.2 Web API:在StartUp中ConfiregeServices这样设置
public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer())); }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }
3.3 对付Asp.Net Core>=3.0 MVC:在StartUp中ConfiregeServices这样设置
services.AddRouting(option => { option.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer); });
路由如下设置:
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 对付Asp.Net Core>=3.0 Web API:在StartUp中ConfiregeServices这样设置
services.AddControllers(options => { options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer())); });
3.5 对付Asp.Net Core>=3.0 Razor Pages:在StartUp中ConfiregeServices这样设置
services.AddRazorPages(options => { options.Conventions.Add(new PageRouteTransformerConvention(new SlugifyParameterTransformer())); });
这样会使/Sys/UserList路由变为/sys/user-list
3.6 对付上面MVC项目,路由模板要调解许多,其实还可以通过实现IControllerModelConvention来实现。public class DashedRoutingConvention : IControllerModelConvention { public void Apply(ControllerModel controller) { var hasRouteAttributes = controller.Selectors.Any(selector => selector.AttributeRouteModel != null); if (hasRouteAttributes) { // This controller manually defined some routes, so treat this // as an override and not apply the convention here. return; } foreach (var controllerAction in controller.Actions) { foreach (var selector in controllerAction.Selectors.Where(x => x.AttributeRouteModel == null)) { var template = new StringBuilder(); if (controllerAction.Controller.ControllerName != "Home") { template.Append(PascalToKebabCase(controller.ControllerName)); } if (controllerAction.ActionName != "Index") { template.Append("https://www.jb51.net/" + PascalToKebabCase(controllerAction.ActionName)); } selector.AttributeRouteModel = new AttributeRouteModel() { Template = template.ToString() }; } } } public static string PascalToKebabCase(string value) { if (string.IsNullOrEmpty(value)) return value; return Regex.Replace( value, "(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z])", "-$1", RegexOptions.Compiled) .Trim() .ToLower(); } }
在StartUp中ConfiregeServices这样设置
public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddMvc(options => options.Conventions.Add(new DashedRoutingConvention())); }
译者:realyrare
出处:https://www.cnblogs.com/mhg215/
以上就是ASP.NET Core自动生成小写破折号路由的实现要领的具体内容,更多关于ASP.NET Core生成小写破折号路由的资料请存眷剧本之家其它相关文章!
您大概感乐趣的文章: