How do you enforce lowercase routing in ASP.NET Co

2020-02-16 08:46发布

问题:

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.NET Core for achieving this. I'd think it would be here:

app.UseMvc(configureRoutes =>
{
    configureRoutes.MapRoute("Default", "{controller=App}/{action=Index}/{id?}");
});

But nothing in configureRoutes looks to allow it... unless there's an extension method somewhere that I can't find in the docs perhaps?

回答1:

For ASP.NET Core:

Add the following line to the ConfigureServices method of the Startup class.

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

Thanks to Skorunka for the answer as a comment. I thought it was worth promoting to an actual answer.



回答2:

As other answers indicate, adding:

services.Configure<RouteOptions>(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<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();
});

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.



回答3:

Found the solution.

In the assembly: Microsoft.AspNet.Routing, and the Microsoft.Extensions.DependencyInjection namespace, you can do this in your ConfigureServices(IServiceCollection services) method:

services.ConfigureRouting(setupAction =>
{
    setupAction.LowercaseUrls = true;
});


回答4:

Update in ASP.NET Core 2.2

From ASP.NET Core 2.2, along with lowercase you can also make your route dashed using ConstraintMap which will make your route /Employee/EmployeeDetails/1 to /employee/employee-details/1 instead of /employee/employeedetails/1.

To do so, in the ConfigureServices method of the Startup class:

services.AddRouting(option =>
{
    option.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
    option.LowercaseUrls = true;
});

And the SlugifyParameterTransformer class should be as follows:

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();
    }
}

And Route configuration should be as follows:

app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller:slugify}/{action:slugify}/{id?}",
                defaults: new { controller = "Home", action = "Index" });
        });

This is will make /Employee/EmployeeDetails/1 route to /employee/employee-details/1



回答5:

For identity, @Jorge Yanes Diez answer doesn't work in ASP.NET Core 2.2 (I think 2.x), so if you use Identity and ASP.NET Core 2.2 (2.x) here is the solution:

services.ConfigureApplicationCookie(options =>
{
    options.LoginPath = "/account/login";
    options.ReturnUrlParameter = "returnurl";
    ...
});

Ref: Configure ASP.NET Core Identity



回答6:

I had this on RegisterRoutes::RouteConfig:

routes.LowercaseUrls = true;