在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
创建动态路由转换对象继承 DynamicRouteValueTransformer public class SlugRouteValueTransformer : DynamicRouteValueTransformer { public SlugRouteValueTransformer() { } public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values) { var requestPath = httpContext.Request.Path.Value; if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/') { // Trim the leading slash requestPath = requestPath.Substring(1); } if (string.IsNullOrEmpty(requestPath)) { return new RouteValueDictionary { { "area", "Core" }, { "controller", "Home" }, { "action", "Index" }, { "id", "" } }; }
在startup中插入动态路由 services.AddScoped<SlugRouteValueTransformer>();
app.UseEndpoints(endpoints => { endpoints.MapDynamicControllerRoute<SlugRouteValueTransformer>("/{**slug}"); endpoints.MapControllerRoute( name: "areas", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论