Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Colon in URL

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.

like image 222
Joe Morgan Avatar asked Jan 14 '11 18:01

Joe Morgan


People also ask

Is Colon allowed in URL path?

It is completely fine to use a colon : in a URL path.

How do you send a colon in a URL?

Colon IS an invalid character in URL unless it is used for its purpose (for eg http://). "...Only alphanumerics [0-9a-zA-Z], the special characters "$-_. +! *'()," [not including the quotes - ed], and reserved characters used for their reserved purposes may be used unencoded within a URL."

What is URL pattern in MVC?

Typical URL Patterns in MVC Applications URL patterns for routes in MVC Applications typically include {controller} and {action} placeholders. When a request is received, it is routed to the UrlRoutingModule object and then to the MvcHandler HTTP handler.

How to Add Controller in MVC?

In Solution Explorer, right-click the Controllers folder and then click Add, then Controller. In the Add Scaffold dialog box, click MVC 5 Controller - Empty, and then click Add. Name your new controller "HelloWorldController" and click Add.


1 Answers

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.

like image 51
frankhommers Avatar answered Oct 02 '22 12:10

frankhommers