How do you enforce lowercase routing in ASP.NET Core?

前端 未结 6 1874
Happy的楠姐
Happy的楠姐 2020-12-04 12:11

In ASP.NET 4 this was as easy as routes.LowercaseUrls = true; in the RegisterRoutes handler for the app.

I cannot find an equivalent in ASP

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-04 12:16

    As other answers indicate, adding:

    services.Configure(options => options.LowercaseUrls = true);
    

    before

    services.AddMvc(...)
    

    works great, but I also want to add that if you use Identity, you will also need:

    services.AddIdentity(options =>
    {
        var appCookie = options.Cookies.ApplicationCookie;
        appCookie.LoginPath = appCookie.LoginPath.ToString().ToLowerInvariant();
        appCookie.LogoutPath = appCookie.LogoutPath.ToString().ToLowerInvariant();
        appCookie.ReturnUrlParameter = appCookie.ReturnUrlParameter.ToString().ToLowerInvariant();
    });
    

    And obviously, replace both IdentityUser, and IdentityRole with your own classes if required.

    I just tested this with .NET Core SDK 1.0.4 and the 1.0.5 runtime.

提交回复
热议问题