Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.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# 如何逃避url编码?_C#_Asp.net_Url_Decode_Encode - Fatal编程技术网

C# 如何逃避url编码?

C# 如何逃避url编码?,c#,asp.net,url,decode,encode,C#,Asp.net,Url,Decode,Encode,我正在创建一个链接,该链接创建包含带有URL参数的链接的URL参数。 问题是我有这样一个链接 http://mydomain/_layouts/test/MyLinksEdit.aspx?auto=true&source= http://vtss-sp2010hh:8088/AdminReports/helloworld.aspx?pdfid=193 &url=http://vtss-sp2010hh:8088/AdminReports/helloworld.aspx?p

我正在创建一个链接,该链接创建包含带有URL参数的链接的URL参数。 问题是我有这样一个链接

http://mydomain/_layouts/test/MyLinksEdit.aspx?auto=true&source=
    http://vtss-sp2010hh:8088/AdminReports/helloworld.aspx?pdfid=193
&url=http://vtss-sp2010hh:8088/AdminReports/helloworld.aspx?pdfid=193%26pdfname=5.6%20Upgrade
&title=5.6 Upgrade
此链接将转到书签添加页面,在该页面中读取这些参数

auto
是指是否读取以下参数

source
是您完成添加或取消后要去的地方

url
是书签链接

title
是书签的名称

url
title
的值被输入到两个字段中。然后用户必须单击
保存
取消
。 问题是,当书签页面在字段中输入值时,它将对其进行解码。 然后,如果您尝试保存,它将不允许您保存,因为
url
值中的
pdfname
值中有一个空格。它需要链接不包含任何空格。所以基本上,我希望它在字段中输入后,仍然是一个
%20
,而不是一个空格

source
auto
title
没有问题,只有
url

有办法解决这个问题吗?比如我可以为
%20
使用一个特殊的转义字符

注意:我无法修改书签页面。

我正在使用c#/asp.net创建链接并转到它


谢谢

在您交出url之前,只需使用HttpUtilty的
UrlEncode
方法

 string encoded = HttpUtility.UrlEncode(url);

由于.NET Framework 4.5,您可以使用
WebUtility.UrlEncode

它驻留在
System.dll
中,因此不需要任何其他引用

它正确地转义URL的字符,不像
Uri.EscapeUriString

Uri.EscapeDataString
不同,它对字符串的长度没有任何限制,因此可以用于POST请求

 System.Net.WebUtility.UrlEncode(urlText)
另一个选择是

 System.Uri.EscapeDataString() 
与UrlEncode/UrlDecode方法相比,Uri.EscapeDataString()和Uri.UnescapeDataString()是安全的,并且在解码时不会将加号字符转换为空格


来自另一个用户的一些详细信息:

我尝试了这个,但它只是像用
+
一样对它进行编码,这不起作用,因为书签页面对它进行了解码。但我发现了,我只是做了,
string.replace(“,%2520”)。这样,书签页面将
%25
解码为
%
,只剩下
%20
,这就是有效的方法。请解释您的答案。目前你所说的是主观的,因为你没有解释原因。为什么它们是安全的…请为问题作者详细说明。与UrlEncode/URLEDECODE方法相比,Uri.EscapeDataString/Uri.UnescapeDataString不会将加号字符转换为空格。(我编辑了我的帖子以澄清答案)