Previous     Next

Allowing special characters (forward slash, hash, asterisk etc) in ASP.Net MVC URL parameters

I’ve been getting into ASP.Net MVC a lot lately and there is plenty that is good about it.

One thing that is not good is the problems that MVC has when you have a special character (*,/,& etc) in your querystring.

Eg: Say you want to pass the param


MVC won’t like it - ‘/’ is a special character reserved as a separator for routes. But standard URL encoding it won’t work either:

MVC will still give a ‘HTTP Error 400 - Bad Request error’.

After looking around, it seems this error has been coming up a bit here and here and Phil Haack explains why standard encoding won’t work here.

One answer is to use base 64 encoding on any parameters that might contain the special characters.

        Public Function MyURLEncode(ByVal sInString As String) As String
                Dim sInStringNoSpaces As String = sInString.Replace(" ", "")
                Dim sURLEncoded As String = HttpUtility.UrlEncode(sInString)

                If sURLEncoded.Replace("+", "") = sInString.Replace(" ", "") Then
                        Return sURLEncoded
                        Return "=" & ToBase64(sInString)
                End If
        End Function

        Public Function MyURLDecode(ByVal sEncodedString As String) As String
                If sEncodedString.StartsWith("=") = True Then
                        MyURLDecode = FromBase64(Mid$(sEncodedString, 2))
                        MyURLDecode = sEncodedString.Replace("+", " ")
                End If
        End Function

        Private Function ToBase64(ByVal sInString As String) As String
                Dim btByteArray As Byte()
                Dim a As New System.Text.ASCIIEncoding()
                Dim sResult As String

                btByteArray = a.GetBytes(sInString)

                sResult = System.Convert.ToBase64String(btByteArray, 0, btByteArray.Length)
                sResult = sResult.Replace("+", "-").Replace("/", "_")
                ToBase64 = sResult
        End Function

        Private Function FromBase64(ByVal sBase64String As String) As String
                sBase64String = sBase64String.Replace("-", "+").Replace("_", "/")
                Dim obj As New ASCIIEncoding()
                FromBase64 = obj.GetString(Convert.FromBase64String(sBase64String))
        End Function

Because one of the useful things about MVC is the SEO-friendly URLs, the code doesn’t convert to Base 64 unless it is necessary.

To use these functions, here is a brief code snippet:

Public Class HotelsController
        Inherits System.Web.Mvc.Controller

        Function SelectHotelList(ByVal City As String, _
        ByVal Location As String)

                oSearch = New With {.City = MyURLEncode(City), .Location = MyURLEncode(Location)}
                Dim oDO As New RouteValueDictionary(oSearch)
                Return RedirectToAction("List", "Hotels", oDO)
        End Function

        Function List(ByVal City As String, _
                                    ByVal Location As String)

                City = MyURLDecode(City)
                Location = MyURLDecode(Location)


        End Function
End Class

Update: As Justin pointed out below, ‘/’ is a valid character in the base64 alphabet. I have had a quick read about Modified Base64 for URL which basically replaces ‘+’ and ‘/’ characters with ‘-’ and ‘_’ (I leave the padding ‘=’, but you can remove them if you want). I have updated the code with these changes, however I can’t find a test case that puts ‘/’ into the base64 encoded string - anyone know of one?

Also, the above code only encodes ASCII values, if you want to encode UNICode strings, then swap ASCIIEncoding with UnicodeEncoding.

This post brought to you by - Over 10,000 Aged domains for sale!

Related posts:

  1. SEOMoz Linkscape API wrapper in VB.Net
  2. Remove HTML tags and Javascript from pages
  3. Automate FireFox with

If you liked this, then subscribe to my RSS feed