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?
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.
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.
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.
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.
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;
});
}
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;
});
}
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With