![](/img/trans.png)
[英]ASP.NET Core RazorPages to force AnchorTagHelper (asp-page) to use lowercase routes
[英]Automatically generate lowercase dashed routes in ASP.NET Core
ASP.NET Core 默认使用http://localhost:5000/DashboardSettings/Index这样的 CamelCase-Routes。 但我想使用小写路由,由破折号分隔: http://localhost:5000/dashboard-settings/index它们更常见和一致,因为我的应用程序扩展了一个运行 Wordpress 的网站,它也有小写 url破折号。
我了解到我可以使用路由选项将 url 更改为小写:
services.ConfigureRouting(setupAction => {
setupAction.LowercaseUrls = true;
});
这有效,但给了我没有任何定界符的 url,例如http://localhost:5000/dashboardsettings/index可读性差。 我可以使用 route 属性定义自定义路由
[Route("dashboard-settings")]
class DashboardSettings:Controller {
public IActionResult Index() {
// ...
}
}
但这会导致额外的工作并且容易出错。 我更喜欢搜索大写字符的自动解决方案,在它们之前插入破折号并将大写字符变为小写。 对于旧的 ASP.NET 这不是什么大问题,但是在 ASP.NET Core 上我看不出如何处理这个问题。
在这里做这个的方法是什么? 我需要某种接口,我可以在其中生成 url(例如标签助手)并用破折号分隔符替换 CamelCase。 然后我需要另一种路由接口,以便将破折号分隔符 url 转换回 CamelCase,以便与我的控制器/操作名称正确匹配。
为此,首先创建的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();
}
}
对于 ASP.NET Core 2.2 MVC:
在Startup
类的ConfigureServices
方法中:
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" });
});
对于 ASP.NET Core 2.2 Web API:
在Startup
类的ConfigureServices
方法中:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer()));
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
对于 ASP.NET Core >=3.0 MVC:
在Startup
类的ConfigureServices
方法中:
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" });
});
对于 ASP.NET Core >=3.0 Web API:
在Startup
类的ConfigureServices
方法中:
services.AddControllers(options =>
{
options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer()));
});
对于 ASP.NET Core >=3.0 Razor 页面:
在Startup
类的ConfigureServices
方法中:
services.AddRazorPages(options =>
{
options.Conventions.Add(new PageRouteTransformerConvention(new SlugifyParameterTransformer()));
});
这将使/Employee/EmployeeDetails/1
路由到/employee/employee-details/1
这里的聚会有点晚了,但是..可以通过实现 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("/" + 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.cs中注册
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc(options => options.Conventions.Add(new DashedRoutingConvention()));
}
可以在这里找到更多信息和示例https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing
我正在使用 Asp.NetCore 2.0.0 和 Razor Pages(不需要显式控制器),所以只需要:
启用小写网址:
services.AddRouting(options => options.LowercaseUrls = true);
创建一个名为Dashboard-Settings.cshtml
的文件,生成的路由变为/dashboard-settings
复制自 ASP.NET Core 3.0(与 2.2 相同) 文档:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(options =>
{
options.Conventions.Add(new RouteTokenTransformerConvention(
new SlugifyParameterTransformer()));
});
}
public class SlugifyParameterTransformer : IOutboundParameterTransformer
{
public string TransformOutbound(object value)
{
if (value == null) { return null; }
// Slugify value
return Regex.Replace(value.ToString(), "([a-z])([A-Z])", "$1-$2").ToLower();
}
}
感谢您提供信息,但是最好过滤选择器,以便跳过那些带有自定义路由模板的选择器:例如[HttpGet("/[controller]/{id}")]
)
foreach (var selector in controllerAction.Selectors
.Where(x => x.AttributeRouteModel == null))
有关最新的方法,请参阅文档。 以下是 ASP.NET Core 7.0 的操作方法:
// IMPORTS
using System.Text.RegularExpressions;
namespace YourApi {
public class Program {
public static void Main(string[] args) {
// replace builder.Services.AddControllers() with the following
builder.Services.AddControllersWithViews(options => {
options.Conventions.Add(new RouteTokenTransformerConvention(
new SlugifyParameterTransformer()));
});
// EXISTING CODE
}
}
// https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing#use-a-parameter-transformer-to-customize-token-replacement
public class SlugifyParameterTransformer : IOutboundParameterTransformer {
public string? TransformOutbound(object? value) {
if (value == null) { return null; }
return Regex.Replace(value.ToString(),
"([a-z])([A-Z])",
"$1-$2",
RegexOptions.CultureInvariant,
TimeSpan.FromMilliseconds(100)).ToLowerInvariant();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.