解码context.Request中的查询字符串

rob*_*ert 3 .net c# asp.net windows-server-2008

我有一个ashx文件,需要一些查询字符串值来提供适当的图像.

问题是一些搜索引擎urlencode然后htmlencode他们的缓存中的那些网址或当他们索引那些.

所以例如而不是得到

/preview.ashx?file=abcd.jpg&root=small
Run Code Online (Sandbox Code Playgroud)

我明白了

/preview.ashx?file=abcd.jpg&root=small
Run Code Online (Sandbox Code Playgroud)

这基本上抛弃了context.Request.QueryString["root"]所以它认为没有变量

我想查询字符串中的第二个键是amp; root, 即&符号后面的部分.

我想知道的是,如果有一种方法可以自动html和urldecode服务器端的查询字符串,以便程序不会混淆.

L.B*_*L.B 11

呼叫HttpUtility.HtmlDecodeHttpUtility.UrlDecode不止一次没有害处.

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


xan*_*ded 8

要对URL进行编码和解码,可以使用以下方法:

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

string decoded = System.Web.HttpUtility.UrlDecode(url);
Run Code Online (Sandbox Code Playgroud)

过去我曾见过查询字符串已被双重编码的实例。在这种情况下,您需要双重解码-这可能是您的问题...