在ASP.NET Core中自动生成小写的虚线路由 [英] Automatically generate lowercase dashed routes in ASP.NET Core
问题描述
ASP.NET Core使用CamelCase-Routes,例如 http:// localhost:5000 / DashboardSettings / Index 默认。但是我想使用小写的路由,这些路由用破折号分隔: http:// localhost:5000 / dashboard-settings / index 更为常见且一致,这是因为我的应用程序扩展了一个运行Wordpress的网站,该网站还带有带短划线的小写网址。
ASP.NET Core uses CamelCase-Routes like http://localhost:5000/DashboardSettings/Index by default. But I want to use lowercase routes, which are delimitted by dashes: http://localhost:5000/dashboard-settings/index They're more common and consistent, cause my application extends a website running Wordpress, which also has lowercase urls with dashes.
我了解到可以使用路由选项将网址更改为小写:
I learned that I can change the urls to lowercase using the routing-options:
services.ConfigureRouting(setupAction => {
setupAction.LowercaseUrls = true;
});
此方法有效,但提供了没有任何分隔符的url,如 http:// localhost:5000 / dashboardsettings / index 可读性很差。我可以使用
This works but gave me urls without any delimiter like http://localhost:5000/dashboardsettings/index which are badly readable. I could define custom routes using the route attribute like
[Route("dashboard-settings")]
class DashboardSettings:Controller {
public IActionResult Index() {
// ...
}
}
但这会导致额外的工作,并且容易出错。我更喜欢一种自动解决方案,该方案搜索大写字符,在其前面插入一个破折号,并使大写字符变为小写。对于旧的ASP.NET,这不是一个大问题,但是在ASP.NET Core上,我看不到如何处理此问题的方向。
But that causes extra-work and is error-prone. I would prefer an automatic solution which search for uppercase chars, insert a dash before them and make the uppercase-char lowercase. For the old ASP.NET this was not a big issue, but on ASP.NET Core I see no direction how to handle this.
在这里做什么?我需要某种接口,可以在其中生成url(例如标记帮助程序),并在其中用破折号分隔符替换CamelCase。然后,我需要用于路由的另一种接口,以便将破折号分隔符url转换回CamelCase以与我的控制器/动作名称正确匹配。
Whats the way to do this here? I need some kind of interface where I can generate urls (like for the tag helpers) and replace there the CamelCase by dash-delimiters. Then I need another kind of interface for the routing, so that the dash-delimiter urls are converted back to CamelCase for correct matching with my controller/action names.
推荐答案
这里的聚会有点晚了,但是..
可以通过实现IControllerModelConvention来做到这一点。
A little late to the party here but.. Can do this by implementing 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中注册它
Then registering it in 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
Can find more info and example here https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing
这篇关于在ASP.NET Core中自动生成小写的虚线路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!