我有一个 ashx 文件,它需要一些查询字符串值来传送适当的图像。
问题是一些搜索引擎 urlencode 然后 htmlencode 这些 url 在他们的缓存中或当他们索引这些时。
例如,而不是获取
/preview.ashx?file=abcd.jpg&root=small
我明白了
/preview.ashx?file=abcd.jpg&root=small
这基本上抛弃了 context.Request.QueryString["root"]
所以它认为没有变量 root
我猜查询字符串中的第二个键是 amp;root 即 & 符号之后的部分。
我想知道是否有一种方法可以在服务器端自动对查询字符串进行 html 和 urldecode,这样程序就不会混淆。
最佳答案
多次调用 HttpUtility.HtmlDecode
或 HttpUtility.UrlDecode
没有坏处。
string queryString = "/preview.ashx?file=abcd.jpg&root=small";
var parsedString = HttpUtility.HtmlDecode(queryString);
var root = HttpUtility.ParseQueryString(parsedString)["root"];
关于c# - 在 context.Request 中解码查询字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12324735/