mar*_*tch 62 asp.net-core-mvc asp.net-core
在ASP.NET 4中,这与应用程序routes.LowercaseUrls = true;的RegisterRoutes处理程序一样简单.
我无法在ASP.NET Core中找到实现此目的的等价物.我想它会在这里:
app.UseMvc(configureRoutes =>
{
configureRoutes.MapRoute("Default", "{controller=App}/{action=Index}/{id?}");
});
Run Code Online (Sandbox Code Playgroud)
但configureRoutes看起来没什么可以允许的......除非在某个地方找到一个我在文档中找不到的扩展方法呢?
cra*_*mes 144
对于ASP.NET Core:
将以下行添加到类的ConfigureServices方法中Startup.
services.AddRouting(options => options.LowercaseUrls = true);
Run Code Online (Sandbox Code Playgroud)
感谢Skorunka作为评论的答案.我认为值得推广到一个实际的答案.
Jor*_*iez 14
正如其他答案所示,补充:
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
Run Code Online (Sandbox Code Playgroud)
之前
services.AddMvc(...)
Run Code Online (Sandbox Code Playgroud)
效果很好,但我还想补充一点,如果你使用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();
});
Run Code Online (Sandbox Code Playgroud)
显然,如果需要,可以替换它们IdentityUser,并IdentityRole使用您自己的类.
我刚用.NET Core SDK 1.0.4和1.0.5运行时测试了这个.
mar*_*tch 12
找到了解决方案.
在程序集:Microsoft.AspNet.Routing和Microsoft.Extensions.DependencyInjection命名空间中,您可以在ConfigureServices(IServiceCollection services)方法中执行以下操作:
services.ConfigureRouting(setupAction =>
{
setupAction.LowercaseUrls = true;
});
Run Code Online (Sandbox Code Playgroud)
Tan*_*jel 10
从ASP.NET Core 2.2开始,您还可以使用小写字母来ConstraintMap使路由/Employee/EmployeeDetails/1变为虚线,从而使用/employee/employee-details/1代替/employee/employeedetails/1。
为此,在类的ConfigureServices方法中Startup:
services.AddRouting(option =>
{
option.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
option.LowercaseUrls = true;
});
Run Code Online (Sandbox Code Playgroud)
并且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();
}
}
Run Code Online (Sandbox Code Playgroud)
并且路由配置应如下:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller:slugify}/{action:slugify}/{id?}",
defaults: new { controller = "Home", action = "Index" });
});
Run Code Online (Sandbox Code Playgroud)
这将成为/Employee/EmployeeDetails/1通往/employee/employee-details/1
Ama*_*l K 10
值得注意的是,设置:
services.Configure<RouteOptions>(options => options.LowercaseUrls = true);
Run Code Online (Sandbox Code Playgroud)
不影响查询字符串。
要确保查询字符串也是小写,请将设置options.LowercaseQueryStrings为true:
services.Configure<RouteOptions>(options =>
{
options.LowercaseUrls = true;
options.LowercaseQueryStrings = true;
});
Run Code Online (Sandbox Code Playgroud)
但是,将此属性设置为true仅当options.LowercaseUrlsis also时才相关true。如果是 则options.LowercaseQueryStrings属性被忽略。options.LowercaseUrlsfalse
| 归档时间: |
|
| 查看次数: |
14351 次 |
| 最近记录: |