如何在ASP.NET Core中强制执行小写路由?

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作为评论的答案.我认为值得推广到一个实际的答案.

  • 值得注意的是,你应该在你的`Startup.ConfigureServices()`方法中实际调用`AddMvc()`之前放置它.`AddMouting()`也是由`AddMvc()`调用`使用方法的`Try`变体来为服务集合添加依赖项.因此,当它看到已经添加了路由依赖关系时,它将跳过`AddMvc()`设置逻辑的那些部分. (26认同)
  • 将其切换为正确答案,因为我的答案是在 asp 4 到 core 的过渡期间。 (2认同)
  • 我相信这是围绕 .NET Core 3.x 完成的。他们对其进行了更改,使路由成为一个独立的功能,而不是与 MVC 捆绑在一起。我不相信路由是从“AddMvc”(或者“AddControllersWithViews”,如果您不需要 RazorPages)调用的。因此,仅当您使用 AspNetCore 2 时,顺序才真正重要。(不记得这是否是 1.x 中的选项)。然而,他们“确实”将小写行为分为两个设置,因此如果您想要完全小写地址,则需要将“LowercaseUrls”和“LowercaseQueryStrings”都设置为“true”。 (2认同)

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运行时测试了这个.

  • 配置 &lt;RouteOptions&gt;() 是最好的答案恕我直言:微小而直截了当(在 mvc core 3.1 上测试) (2认同)

mar*_*tch 12

找到了解决方案.

在程序集:Microsoft.AspNet.RoutingMicrosoft.Extensions.DependencyInjection命名空间中,您可以在ConfigureServices(IServiceCollection services)方法中执行以下操作:

services.ConfigureRouting(setupAction =>
{
    setupAction.LowercaseUrls = true;
});
Run Code Online (Sandbox Code Playgroud)

  • 对于ASP NET MVC CORE:services.AddRouting(options => {options.LowercaseUrls = true;}); (14认同)
  • 在RTM之前就是这样,现在你应该使用.AddRouting而不是.ConfigureRouting (3认同)

Tan*_*jel 10

在ASP.NET Core 2.2中更新

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.LowercaseQueryStringstrue

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