Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在context.Request中解码查询字符串_C#_Asp.net_.net_Windows Server 2008 - Fatal编程技术网

C# 在context.Request中解码查询字符串

C# 在context.Request中解码查询字符串,c#,asp.net,.net,windows-server-2008,C#,Asp.net,.net,Windows Server 2008,我有一个ashx文件,它需要一些查询字符串值来传递适当的图像 问题是一些搜索引擎先对URL进行编码,然后在缓存中或索引时对这些URL进行htmlencode 因此,比如说,与其 /preview.ashx?file=abcd.jpg&root=small 我明白了 这基本上摆脱了context.Request.QueryString[“root”],因此它认为没有变量root 我猜查询字符串中的第二个键是amp;根即&符号后面的部分 我想知道的是,是否有一种方法可以在服务器端自动对查询

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

问题是一些搜索引擎先对URL进行编码,然后在缓存中或索引时对这些URL进行htmlencode

因此,比如说,与其

/preview.ashx?file=abcd.jpg&root=small
我明白了

这基本上摆脱了
context.Request.QueryString[“root”]
,因此它认为没有变量root

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


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

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

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

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

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

多次调用
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
-你的意思是
C#
?不应该是:
context.Request.QueryString[“root”]
(字符串
root
)是的。。我是说c#。。。context.Request.QueryString[“root”]应该返回small,但它返回null,因为没有像root这样的键。。。。有一个钥匙放大器;谢谢。。。我其实是想避免那样做。。。但我想既然解码后无法设置context.Request.QueryString,那也应该可以。我想知道是否有一种方法可以在服务器端对其进行解码,这样我就可以将程序排除在it之外。如果您的原始URL中有一个编码的+符号,并且您调用了decode两次,这不是一个问题吗?第一次,它将被解码为+,但第二次,+将被解码为一个空格。除非我遗漏了什么?@rar不如发布一个使用for ex
HttpUtility.UrlEncode
HttpUtility.UrlDecode
的示例,这样我们就可以讨论具体的示例代码了。
string queryString = "/preview.ashx?file=abcd.jpg&root=small";
var parsedString = HttpUtility.HtmlDecode(queryString);
var root = HttpUtility.ParseQueryString(parsedString)["root"];