robert robert - 1 year ago 104
ASP.NET (C#) Question

decode querystring within context.Request

i have an ashx file that requires some query-string values to deliver appropriate images.

The problem is some search engines urlencode then htmlencode those urls in their cache or when they index those.

So for example instead of getting


i get


this basically throws off the
so it thinks that there's no variable root

i guess the second key in the querystring is amp;root i.e the part after the & sign.

What i'm wondering is if there's a way to automatically html and urldecode the querystring on serverside so that the program doesn't get confused.

Answer Source

There is no harm in calling HttpUtility.HtmlDecode or HttpUtility.UrlDecode more than once.

string queryString = "/preview.ashx?file=abcd.jpg&root=small";
var parsedString = HttpUtility.HtmlDecode(queryString);
var root = HttpUtility.ParseQueryString(parsedString)["root"];