views:

22

answers:

1

Hi All, i am attempting to use HttpUtility.UrlEncode to encode strings that ultimately are used in URLs.

example

/string/http://www.google.com

or

/string/my test string

where http://www.google.com is a parameter passed to a controller.

I have tried UrlEncode but it doesn't seem to work quite right

my route looks like:

routes.MapRoute(
            "mStringView",
            "mString/{sText}",
            new { controller = "mString", action = "Index", sText = UrlParameter.Optional }
        ); 

The problem is the encoded bits are decoded it seems somewhere in the routing.. except things like "+" which replace " " are not decoded..

Understanding my case, where a UrlParameter can be any string, including URL's.. what is the best way to encode them before pushing them into my db, and then handling the decode knowing they will be passed to a controller as a parameter?

thanks!

A: 

It seems this problem has come up in other forums and the general recommendation is to not rely on standard url encoding for asp.net mvc. The advantage is url encoding is not necessarily as user friendly as we want, which is one of the goals of custom routed urls. For example, this:

http://server.com/products/Goods+%26+Services

can be friendlier written as

http://server.com/products/Good-and-Services

So custom url encoding has advantages beyond working around this quirk/bug. More details and examples here:

http://www.dominicpettifer.co.uk/Blog/34/asp-net-mvc-and-clean-seo-friendly-urls

Sam