5

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

问题是一些搜索引擎 urlencode 然后 htmlencode 在他们的缓存中或当他们索引这些 url 时。

所以例如而不是得到

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

我明白了

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

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

我猜查询字符串中的第二个键是amp;root 即 & 符号之后的部分。

我想知道是否有一种方法可以在服务器端自动对查询字符串进行 html 和 urldecode,这样程序就不会混淆。

4

2 回答 2

12

HttpUtility.HtmlDecode打电话或HttpUtility.UrlDecode不止一次打电话没有害处。

string queryString = "/preview.ashx?file=abcd.jpg&root=small";
var parsedString = HttpUtility.HtmlDecode(queryString);
var root = HttpUtility.ParseQueryString(parsedString)["root"];
于 2012-09-07T20:17:31.337 回答
10

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

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

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

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

于 2012-09-07T20:13:51.120 回答