在 ASP.NET 4 中,这与应用程序的 RegisterRoutes
处理程序中的 routes.LowercaseUrls = true;
一样简单。
我在 ASP.NET Core 中找不到实现此目的的等价物。我想它会在这里:
app.UseMvc(configureRoutes =>
{
configureRoutes.MapRoute("Default", "{controller=App}/{action=Index}/{id?}");
});
但是 configureRoutes
中没有任何内容看起来允许它......除非在某个地方有一个我在文档中找不到的扩展方法?
对于 ASP.NET 核心:
将以下行之一添加到 Startup
类的 ConfigureServices
方法:
services.AddRouting(options => options.LowercaseUrls = true);
或者
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
感谢 Skorunka 作为评论的回答。我认为值得推广到一个实际的答案。
更新 ASP.NET Core 版本 >= 2.2
从 ASP.NET Core 2.2 开始,除了 lowercase,您还可以使用 ConstraintMap
将您的 路由虚线 设为 /Employee/EmployeeDetails/1
到 /employee/employee-details/1
而不是 /employee/employeedetails/1
。
为此,首先创建的 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);
});
并且Route配置应该如下:
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);
});
并且Route配置应该如下:
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
employee-details
。你能告诉我你的配置吗?
正如其他答案所示,添加:
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
前
services.AddMvc(...)
效果很好,但我还想补充一点,如果您使用 Identity,您还需要:
services.AddIdentity<IdentityUser, IdentityRole>(options =>
{
var appCookie = options.Cookies.ApplicationCookie;
appCookie.LoginPath = appCookie.LoginPath.ToString().ToLowerInvariant();
appCookie.LogoutPath = appCookie.LogoutPath.ToString().ToLowerInvariant();
appCookie.ReturnUrlParameter = appCookie.ReturnUrlParameter.ToString().ToLowerInvariant();
});
显然,如果需要,请将 IdentityUser
和 IdentityRole
替换为您自己的类。
我刚刚使用 .NET Core SDK 1.0.4 和 1.0.5 运行时对此进行了测试。
找到了解决方案。
在程序集:Microsoft.AspNet.Routing
和 Microsoft.Extensions.DependencyInjection
命名空间中,您可以在 ConfigureServices(IServiceCollection services)
方法中执行此操作:
services.ConfigureRouting(setupAction =>
{
setupAction.LowercaseUrls = true;
});
值得注意的是,设置:
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
不影响查询字符串。
要确保查询字符串也是小写,请将 options.LowercaseQueryStrings
设置为 true
:
services.Configure<RouteOptions>(options =>
{
options.LowercaseUrls = true;
options.LowercaseQueryStrings = true;
});
但是,仅当 options.LowercaseUrls
也是 true
时,将此属性设置为 true
才有意义。如果 options.LowercaseUrls
是 false
,则 options.LowercaseQueryStrings
属性将被忽略。
对于身份,@Jorge Yanes Diez 答案在 ASP.NET Core 2.2
中不起作用(我认为是 2.x),所以如果您使用身份和 ASP.NET Core 2.2 (2.x),这里是解决方案:
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/account/login";
options.ReturnUrlParameter = "returnurl";
...
});
参考:Configure ASP.NET Core Identity
我在 RegisterRoutes::RouteConfig 上有这个:
routes.LowercaseUrls = true;
不定期副业成功案例分享
Startup.ConfigureServices()
方法中实际调用AddMvc()
之前将其放入。AddRouting()
也由AddMvc()
调用,它使用方法的Try
变体将依赖项添加到您的服务集合。因此,当它看到已添加路由依赖项时,它将跳过AddMvc()
设置逻辑的那部分。AddMvc
(或AddControllersWithViews
,如果您不需要 RazorPages)调用路由。因此,仅当您使用 AspNetCore 2 时,顺序才真正重要。(不记得这是否是 1.x 中的选项)。但是,他们确实将小写行为分为两个设置,因此如果您想要完全小写的地址,则需要将LowercaseUrls
和LowercaseQueryStrings
都设置为true
。