2

I am doing a bad thing here. I am asking a question without trying out first by hopping that someone knows an easy way of doing this.

Any chance we can make ASP.NET MVC Routing system case sensitive? I would like the following two Urls to be different:

example.com/a

example.com/A

Do we have an easy fix or should write our own handler for this.

Charles
  • 50,943
  • 13
  • 104
  • 142
tugberk
  • 57,477
  • 67
  • 243
  • 335
  • 1
    I'd be curious to understand why you need to do this. What's the reasoning for this? – George Stocker Mar 01 '12 at 14:36
  • 3
    Are you looking to make the Controller, Action or other optional parameters case sensitive? All can be done using Route Constraints. If it's just controller/action you want case sensitive a single route constraint could be used but if you wanted to make the optional values case sensitive it may take a different route constraint per route you want to validate. On the other hand, if you're hard coding routes, like /a maps to Home controller Index action and /A maps to Home controller About action, thats even easier, a RegEx route contraint could be used. – Nick Bork Mar 01 '12 at 14:55
  • My purpose of wanting this is to be able to generate lover and upper case random strings for url shortener service. – tugberk Mar 01 '12 at 16:11
  • 1
    @tugberk - I would think the shortened url would be a parameter then, not a route. The parameter can be interpreted as you like. – Erik Funkenbusch Mar 01 '12 at 18:16

3 Answers3

5

I haven't tried it, but I would think that if you use a regular expression route constraint that only matched the uppercase and lowercase seperate, that would work. I don't think it's a good idea though.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
3

Also note that regex options like case sensitivity can be changed mid regex by things like:

...(?-i:nonCaseSensitiveBit)backToCaseInsensitive...

See the Regex docs for the full readout.

ChrisF
  • 180
  • 8
3

That seems like a bad idea. You're assuming that folks will never touch a url by hand, always clicking links.

Plus, by default, MSSQL is case insensitive, assuming that some of your route values are database bound.

I'd also be willing to bet that all urls in the search engines are lower case( Edumacated guess ).

EDIT

All google paid search urls are lower case.

If you STILL insist on doing this:

You also may have to have implement a custom viewengine that looks for the views on a case sensitive basis. Also a custom controller factory to find controllers that are case sensitive.

Darthg8r
  • 12,377
  • 15
  • 63
  • 100
  • The controllers/actions by case sensitive can be done using a IRouteConstraint and reflection or a custom Controller Factory.The RouteConstraint is a better option if the OP is using an ActionLink or even calling Url.Action. – Nick Bork Mar 01 '12 at 15:00
  • Search engines had better not change the case of URLs, that would result in a different URL. See [RFC 3986 6.2.2.1](http://tools.ietf.org/html/rfc3986#section-6.2.2.1) and [RFC 2616 3.2.3](http://tools.ietf.org/html/rfc2616#section-3.2.3) – derkyjadex Apr 11 '12 at 18:20
  • Only domain names are case-insensitive. URI paths are case-sensitive. – Monstieur Feb 26 '13 at 06:37