3

I am developing mv4 website,

would you please tell me why these command do not do anything in my application when I write them into RouteConfig:

routes.LowercaseUrls = true;
routes.AppendTrailingSlash = true;
Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
Sepehr Estaki
  • 331
  • 5
  • 19

2 Answers2

11

First up, you need to be generating the routes via something @Html.ActionLink, @Url.Action or similar for anything to work. It's not a magic bullet.

From MSDN:

AppendTrailingSlash

Gets or sets a value that indicates whether trailing slashes are added when virtual paths are normalized.

(Source)

LowercaseUrls

Gets or sets a value that indicates whether URLs are converted to lower case when virtual paths are normalized.

(Source)

The URL format is (source):

scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]

The portion of that this affects is the path portion and that only. Any query-string portion is unaffected. AppendTrailingSlash adds a / to the end of the path portion.

So for this code:

@Html.ActionLink("About this Website", "About")

With the route parameters set to routes.LowercaseUrls = true; and routes.AppendTrailingSlash = true; it would generate:

<a href="/home/about/">About this Website</a>

With the route parameters set to routes.LowercaseUrls = false; and routes.AppendTrailingSlash = true; it would generate:

<a href="/Home/About/">About this Website</a>

With the route parameters set to routes.LowercaseUrls = true; and routes.AppendTrailingSlash = false; it would generate:

<a href="/home/about">About this Website</a>

With the route parameters set to routes.LowercaseUrls = false; and routes.AppendTrailingSlash = false; it would generate:

<a href="/Home/About">About this Website</a>

For this code:

@url.action("someAction","someController",new{Name=Test})

With the route parameters set to routes.LowercaseUrls = true; and routes.AppendTrailingSlash = true; it would generate:

/somecontroller/someaction/?Name=Test

Some bugs that you could be hitting:
There is a bug with areas in MVC 4, in regards to lowercase Urls.

The NuGet package LowercaseRoutesMVC preports to fix this bug.


Possible solutions to lowercase URLs on your Application

If you're wanting to alter behavior outside of the app, consider using something like Url Rewrite for IIS which can transform your URLs. Example of this here.

Community
  • 1
  • 1
NikolaiDante
  • 18,469
  • 14
  • 77
  • 117
1

I think you are confused as to what these do.

In the case of AppendTrailingSlash all it does is add a / to the end of routes generated by MVC. (https://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.appendtrailingslash(v=vs.110).aspx)

Similarly when you use LowercaseUrls it MVC will generate it's URLS in all lowercase. (https://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.lowercaseurls(v=vs.110).aspx)

These will not do URL rewriting on the client side like your expecting.

Matthew Verstraete
  • 6,335
  • 22
  • 67
  • 123
  • what do you mean will not rewriting on the client side ? then where it rewrite ?! – Sepehr Estaki Jan 15 '16 at 11:12
  • 1
    When you use an HTML helper tag, like `@Html.ActionLink` that is where these changes get applied. When I say on the client side I mean the user browsing your website. These settings will not change the URL in his/her address. – Matthew Verstraete Jan 15 '16 at 13:49