在 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.
我了解到我可以使用路由选项将 url 更改为小写:
I learned that I can change the urls to lowercase using the routing-options:
services.ConfigureRouting(setupAction => {
setupAction.LowercaseUrls = true;
});
这有效,但给了我没有任何分隔符的网址,如 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.
推荐答案
更新 ASP.NET Core 版本 >= 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();
}
}
对于 ASP.NET Core 2.2 MVC:
在Startup
类的ConfigureServices
方法中:
services.AddRouting(option =>
{
option.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
});
并且路由配置应该如下:
And route configuration should be as follows:
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
这篇关于在 ASP.NET Core 中自动生成小写虚线路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!