views:

111

answers:

2

This works fine:

GET /mvc/Movies/TitleIncludes/Lara%20Croft

When I submit a request that contains a colon, like this:

GET /mvc/Movies/TitleIncludes/Lara%20Croft:%20Tomb

...it generates a 400 error. The error says ASP.NET detected invalid characters in the URL.

If I try url-escaping, the request looks like this:

GET /mvc/Movies/TitleIncludes/Lara%20Croft%3A%20Tomb

...and this also gives me a 400 error.

If I replace the colon with a | :

GET /mvc/Movies/TitleIncludes/Lara%20Croft|%20Tomb

..that was also rejeted as illegal, this time with a 500 error. The message: Illegal characters in path.

URL-escaping that | results in the same error.


I really, really don't want to use a querystring parameter.


related:
Sending URLs/paths to ASP.NET MVC controller actions

+1  A: 

I found that URL encoding did not work, but custom encoding did.
I guess ASPNET MVC uses the filesystem to do the parsing and routing, because a character in the URL that is not legal in the filesystem, causes a 500 or 400 error.

So what I did was replace colons with the unicode ¡ character in the javascript side, and then do the converse in the action. like this:

browser:

function myEscape(s){
    return s.replace(':', '%C2%A1').trim();
}

in the action, call this conversion before using the argument:

private string MyCustomUnescape(string arg)
{
    return arg.Replace("¡", ":");
}

The same approach works for slashes - just pick a different unicode character. Of course if your string arguments themselves are unicode, then you'll have to use non-printable characters for the "encoded" forms.

Cheeso
A: 

If SEO is not a problem you may use base64 and then urlencode that. After the first step every character you'll have will be easily encoded. Decoding in .NET is as easy as using the helper in System.Web.HttpUtility and System.Convert.

emaster70