posted on 22 January 2013


Reliable URL encoding in ASP.NET and MVC


We recently ran into some URL encoding issues in one of our MVC projects when it came to custom routing. When encoding the string "IIS 7+" using System.HttpUtility.UrlEncode() the resulting output was "IIS+7%2b" which when decoded gave us "IIS 7 " which is incorrect.

We then played around with System.HttpUtility.UrlPathEncode() in which the resulting output was "IIS%207+" which was breaking in IIS.

The solution, System.Uri.EscapeDataString()

This method appears to be much more reliable compared to the other two methods and did the trick for us. Simply decode using System.Uri.UnescapeDataString()

Furthermore, to enable plus signs in the URL path, the following web.config entry is required.

     <requestfiltering allowdoubleescaping="true" />

As you can see this is a rather quick & simple implementation that takes little to no time when known about.



Adam, a South African born Australian, is both a qualified web developer and sound engineer. He thrives on coding the WWW and his specialties include C#, JavaScript & Service Oriented Architecture.

Find out more about Adam here.


add your comment

Email me when other users reply