![](/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.