Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lower case urls and trailing slash

I am using ASP.NET MVC 6 and am trying to use lower case url's and trailing slash.

I have used the following:

RouteTable.Routes.LowercaseUrls = true;
RouteTable.Routes.AppendTrailingSlash = true;

But it gives a compile error. Just wanted to know if MVC 6 has a different way to do the above or is the same as we did in MVC5?

like image 886
CuriousDev Avatar asked Jan 17 '15 08:01

CuriousDev


People also ask

Should base URL include trailing slash?

The trailing slash does not matter for your root domain or subdomain. Google sees the two as equivalent. But trailing slashes do matter for everything else because Google sees the two versions (one with a trailing slash and one without) as being different URLs.

Should URLs end in trailing slash?

If your site has a directory structure, it's more conventional to use a trailing slash with your directory URLs (for example, example.com/directory/ rather than example.com/directory ), but you can choose whichever you like. Be consistent with the preferred version.

Why do URLs have a trailing slash?

A trailing slash is a forward slash placed at the end of a URL. It's usually used to indicate a directory (as opposed to a file), but in SEO it can affect your rankings. Take a look at the URLs below and guess which one is 'correct'. Note that one of them has a trailing slash at the end.

Can URL contain backslash?

If you try to enter a backslash in a Web address you will likely get an error. They are rarely used in URLs. Thus they shouldn't be used when discussing Web addresses. Backslashes are often used in programming languages and old DOS directories, so on occasion developers and nerds might correctly use the name.


3 Answers

You can now use AddRouting to tell the routing engine to use lowercase URL's. This code goes in the Startup.cs file:

public void ConfigureServices(IServiceCollection services)
{
    // ...ommitted code

    services.AddRouting(options =>
    {
        options.AppendTrailingSlash = true;
        options.LowercaseUrls = true;
    });
}
like image 178
Muhammad Rehan Saeed Avatar answered Oct 16 '22 14:10

Muhammad Rehan Saeed


From the .NET Core 1.0.1 the correct syntax to implement the trailing slash and lowercase urls is:

public void ConfigureServices(IServiceCollection services)
{
    // Omitted code
    // ...

    // Configure routing.
    services.Configure<RouteOptions>(options =>
    {
        options.AppendTrailingSlash = true;
        options.LowercaseUrls = true;
    });
}
like image 43
Giorgio Zanoli Avatar answered Oct 16 '22 13:10

Giorgio Zanoli


Those APIs have not yet been ported from ASP.NET 4.x (used by MVC 5.x and earlier) to ASP.NET 5, which is used by MVC 6.

The feature request to support lowercase URLs is logged here (and isn't yet resolved): https://github.com/aspnet/Routing/issues/140 (please note that it's in the Routing repo because this is a Routing feature and not specific to MVC).

like image 45
Eilon Avatar answered Oct 16 '22 12:10

Eilon