Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

/preview.ashx?file=abcd.jpg&root=small

i get

/preview.ashx?file=abcd.jpg&root=small

this basically throws off the context.Request.QueryString["root"] 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.

like image 557
robert Avatar asked Sep 07 '12 20:09

robert


2 Answers

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"];
like image 61
L.B Avatar answered Nov 04 '22 19:11

L.B


To URL encode and decode you can use the following methods:

string encoded = System.Web.HttpUtility.UrlEncode(url);

string decoded = System.Web.HttpUtility.UrlDecode(url);

I've seen instances in the past where the Query String has been doubly encoded. In which case you'll need to doubly decode — this may be your issue...

like image 45
Alex Avatar answered Nov 04 '22 19:11

Alex