ASP.NET Core自動(dòng)生成小寫破折號(hào)路由的實(shí)現(xiàn)方法
默認(rèn)情況下,ASP.NET Core使用如 http://localhost:5000/HomeIndex 類的大駝峰路由。但是如果想使用小寫的路由,并且這些路由用破折號(hào)分隔:http://localhost:5000/home-index它們比較常見且一致。
舉例.NET常見路由 http://localhost:5000/User/ListPages 想要的效果 http://localhost:5000/user/list-pages
1、如何生成小寫的路由可以這樣設(shè)置
services.ConfigureRouting(setupAction => {
setupAction.LowercaseUrls = true;
});
2、生成帶破折號(hào)并且小寫的路由可以這樣設(shè)置
[Route("dashboard-settings")]
class DashboardSettings:Controller {
public IActionResult Index() {
// ...
}
}
似乎上面使用特性路由可以解決這個(gè)問題。但是我不想使用,因?yàn)槊總€(gè)action都要手動(dòng)去設(shè)置,太繁瑣也很容易出錯(cuò)。
我想要的效果是在程序中寫個(gè)擴(kuò)展類做到可配置處理。
3、解決方案
以下支持Asp.Net Core Version>=2.2
要做到這一點(diǎn),首先創(chuàng)建SlugifyParameterTransformer類應(yīng)該如下所示
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 對(duì)于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 對(duì)于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 對(duì)于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 對(duì)于Asp.Net Core>=3.0 Web API:
在StartUp中ConfiregeServices這樣配置
services.AddControllers(options =>
{
options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer()));
});
3.5 對(duì)于Asp.Net Core>=3.0 Razor Pages:
在StartUp中ConfiregeServices這樣配置
services.AddRazorPages(options =>
{
options.Conventions.Add(new PageRouteTransformerConvention(new SlugifyParameterTransformer()));
});
這樣會(huì)使/Sys/UserList路由變?yōu)?sys/user-list
3.6 對(duì)于上面MVC項(xiàng)目,路由模板要調(diào)整很多,其實(shí)還可以通過實(shí)現(xiàn)IControllerModelConvention來實(shí)現(xiàn)。
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中ConfiregeServices這樣配置
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc(options => options.Conventions.Add(new DashedRoutingConvention()));
}
譯者:realyrare
以上就是ASP.NET Core自動(dòng)生成小寫破折號(hào)路由的實(shí)現(xiàn)方法的詳細(xì)內(nèi)容,更多關(guān)于ASP.NET Core生成小寫破折號(hào)路由的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- ASP.NET?Core使用EF創(chuàng)建模型(包含屬性、排除屬性、主鍵和生成值)
- ASP.NET Core 5中如何生成PDF文檔
- Asp.Net Core使用swagger生成api文檔的完整步驟
- 詳解ASP.NET Core 2.0 路由引擎之網(wǎng)址生成(譯)
- Asp.NetCore1.1版本去掉project.json后如何打包生成跨平臺(tái)包
- Asp.net core WebApi 使用Swagger生成幫助頁(yè)實(shí)例
- 基于ASP.NET Core數(shù)據(jù)保護(hù)生成驗(yàn)證token示例
- asp.net core實(shí)現(xiàn)在線生成多個(gè)文件將多個(gè)文件打包為zip返回的操作
相關(guān)文章
asp.net下將純真IP數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù)中的代碼
純真IP數(shù)據(jù)庫(kù)包含最新的IP信息,通過IP我們可以查詢?cè)L問者的來路,地理位置!但下載下來的IP數(shù)據(jù)無法被我們直接調(diào)用,所以我們需要編寫代碼將IP寫入到數(shù)據(jù)庫(kù)中供我們使用!2011-01-01
asp.net 通用分頁(yè)顯示輔助類(改進(jìn)版)
在使用ASP.NET編程時(shí),如果不用控件的默認(rèn)分頁(yè)功能,想自己搞一個(gè),可以看看本文的asp.net通用分頁(yè)顯示輔助類哦。2010-04-04
asp.net無法加載oci.dll等錯(cuò)誤的解決方法
.net在windows2003下訪問oracle9i提示“無法加載oci.dll”或"無法在dll oci.dll中找到名為ocienvcreate的入口點(diǎn) "的修復(fù)方法2013-10-10
.NET Core 2.0 Preview2 發(fā)布匯總
這篇文章主要為大家詳細(xì)介紹了.NET Core 2.0 Preview2 發(fā)布匯總的相關(guān)內(nèi)容,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
請(qǐng)求如何進(jìn)入ASP.NET MVC框架
這篇文章主要介紹了請(qǐng)求如何進(jìn)入ASP.NET MVC框架的實(shí)現(xiàn)過程,感興趣的小伙伴們可以參考一下2016-04-04
ASP.NET 恢復(fù)備份Sqlserver實(shí)現(xiàn)代碼
在線恢復(fù)和備份SQL Server的代碼,需要的朋友可以參考下。2010-04-04
asp.net 通過指定IP地址得到當(dāng)前的網(wǎng)絡(luò)上的主機(jī)的域名
通過指定的ip地址獲取當(dāng)前網(wǎng)絡(luò)的主機(jī)的域名,大家可以看看2009-02-02
C#反射技術(shù)的簡(jiǎn)單操作(讀取和設(shè)置類的屬性)
反射的作用想必大家都知道了吧,少量屬性的自動(dòng)化操作手動(dòng)添加幾下當(dāng)然是沒有問題的,但是屬性數(shù)量較多的時(shí)候敲起這些繁鎖的代碼可以困了,再說對(duì)擴(kuò)展和維護(hù)性造成很多的不遍,以下代碼中如不能直接使用請(qǐng)?zhí)砑觰sing System.Text;的引用。2011-01-01
asp.net訪問網(wǎng)絡(luò)路徑方法(模擬用戶登錄)
這篇文章主要介紹了asp.net訪問網(wǎng)絡(luò)路徑方法,其實(shí)就是模擬用戶登錄,需要的朋友可以參考下2014-08-08

