6

I've seen that IIS has a problem with letting colons into URLs. I also saw the suggestions others offered here.

With the site I'm working on, I want to be able to pass titles of movies, books, etc., into my URL, colon included, like this:

mysite.com/Movie/Bob:The Return

This would be consumed by my MovieController, for example, as a string and used further down the line.

I realize that a colon is not ideal. Does anyone have any other suggestions? As poor as it currently is, I'm doing a find-and-replace from all colons (:) to another character, then a backwards replace when I want to consume it on the Controller end.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Joe Morgan
  • 1,761
  • 6
  • 20
  • 29
  • Why would you use a colon? Did you find this works on servers other than IIS? I wish colon wasn't even allowed for specifying port numbers because it would make parsing the protocol much simpler. Use a hyphen, underscore, or other character. Or make it another level like `mysite.com/Movie/Bob/The+Return`. – Jonathan Wood Jan 14 '11 at 18:58
  • The issue with doing `mysite.com/Movie/Bob/The+Return` would be figuring out how to take a movie title ("Bob:The Return") and know "Hey, there was a colon here in the title...I need to put that back together properly." – Joe Morgan Jan 14 '11 at 22:33
  • Maybe I should pass in a class that has both the title and the slug in it instead of just the movie title as a string? – Joe Morgan Jan 14 '11 at 22:34

5 Answers5

7

I resolved this issue by adding this to my web.config:

<httpRuntime requestPathInvalidCharacters=""/>

This must be within the system.web section.

The default is:

<httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,%,&amp;,:,\,?"/>

So to only make an exception for the colon it would become

<httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,%,&amp;,\,?"/>

Read more at: http://msdn.microsoft.com/en-us/library/system.web.configuration.httpruntimesection.requestpathinvalidcharacters.aspx

For what I understand the colon character is acceptable as an unencoded character in an URL. I don't know why they added it to the default of the requestPathInvalidCharacters.

frankhommers
  • 1,169
  • 12
  • 26
3

Consider URL encoding and decoding your movie titles.

You'd end up with foo.com/bar/Bob%58The%20Return

As an alternative, consider leveraging an HTML helper to remove URL unfriendly characters in URLs (method is URLFriendly()). The SEO benefits between a colon and a placeholder (e.g. a dash) would likely be negligable.

p.campbell
  • 98,673
  • 67
  • 256
  • 322
  • 10
    I tried doing encoding/decoding. IIS still seemed to see that and say, "Hey, that's still a colon. Throw a 400 error!" – Joe Morgan Jan 14 '11 at 22:35
2

One of the biggest worries with your approach is that the movie name isn't always going to be unique (e.g. "The Italian Job"). Also what about other ilegal characters (e.g. brackets etc).

It might be a good idea to use an id number in the url to locate the movie in your database. You could still include a url friendly copy of movie name in your url, but you wouldn't need to worry about getting back to the original title with all the illegal characters in it.

A good example is the url to this page. You can see that removing the title of the page still works:

ASP.NET MVC Colon in URL

ASP.NET MVC Colon in URL

Community
  • 1
  • 1
Andrew Skirrow
  • 3,402
  • 18
  • 41
0

Colon is a reserved and invalid character in an URI according to the RFC 3986. So don't do something that violates the specification. You need to either URL encode it or use another character. And here's a nice blog post you might take a look at.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Where does it say that it's invalid? Am I interpreting this incorrectly? pchar = unreserved / pct-encoded / sub-delims / ":" / "@" – glen-84 May 21 '12 at 09:08
0

The simplest way is to use System.Web.HttpUtility.UrlEncode() when building the url and System.Web.HttpUtility.UrlDecode when interpreting the results coming back. You would also have problems with the space character if you don't encode the value first.

NerdFury
  • 18,876
  • 5
  • 38
  • 41