C# 如何转义url编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17241677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:56:19  来源:igfitidea点击:

How to escape url encoding?

c#asp.neturldecodeencode

提问by omega

I am creating a link that creates URL parameters that contains links with URL parameters. The issue is that I have a link like this

我正在创建一个链接,该链接创建包含带有 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

This link goes to a bookmark adding page where it reads these parameters.

此链接转到一个书签添加页面,它会在其中读取这些参数。

autois wheather to read the following parameters or not

auto是否读取以下参数

sourceis where to go after you finish adding or cancelling

source是您完成添加或取消后的去处

urlis the bookmark link

url是书签链接

titleis the name of the bookmark

title是书签的名称

The values of urland titleget entered into 2 fields. Then the user has to click saveor cancel. The problem is when the bookmark page enters the values into the field, it will decode them. Then if you try to save, it will won't let you save because the pdfnamevalue in the urlvalue has a space in it. It needs the link to not have any spaces. So basically, I want it so that after it enters it in the field, it will still be a %20instead of a space.

的价值urltitle获得进入2场。然后用户必须单击savecancel。问题是当书签页面在字段中输入值时,它会解码它们。然后,如果您尝试保存,它不会让您保存,因为pdfnamevalue 中的urlvalue 中有一个空格。它需要链接没有任何空格。所以基本上,我想要它,以便在它进入现场后,它仍然是一个%20而不是一个空间。

There isn't a problem with source, auto, or title, just the url...

source, auto, or没有问题title,只是url...

Is there a way to solve this? Like maybe a special escape character I can use for the %20?

有没有办法解决这个问题?就像我可以使用的特殊转义字符%20?

Note: I cannot modify the bookmark page.

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

I am using c#/asp.net to create the link and go to it.

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

Thanks

谢谢

回答by evanmcdonnal

Just use HttpUtilty's UrlEncodemethod right before you hand off the url;

只需UrlEncode在传递 url 之前使用 HttpUtilty 的方法;

 string encoded = HttpUtility.UrlEncode(url);

回答by ericdc

Since .NET Framework 4.5 you can use WebUtility.UrlEncode.

从 .NET Framework 4.5 开始,您可以使用WebUtility.UrlEncode.

It resides in System.dll, so it does not require any additional references.

它驻留在 中System.dll,因此不需要任何额外的引用。

It properly escapes characters for URLs, unlike Uri.EscapeUriString

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

It does not have any limits on the length of the string, unlike Uri.EscapeDataString, so it can be used for POST requests

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

 System.Net.WebUtility.UrlEncode(urlText)

Another option is

另一种选择是

 System.Uri.EscapeDataString() 

回答by mmosoll

Uri.EscapeDataString() and Uri.UnescapeDataString() are safe comparing to UrlEncode/UrlDecode methods and does not convert plus characters into spaces when decoding.

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

Some details from another user: http://geekswithblogs.net/mikehuguet/archive/2009/08/16/134123.aspx

来自另一个用户的一些细节:http: //geekswithblogs.net/mikehuguet/archive/2009/08/16/134123.aspx