C# 用于查询字符串的 ASP.Net URLEncode Ampersand

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/561954/
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-04 08:15:01  来源:igfitidea点击:

ASP.Net URLEncode Ampersand for use in Query String

c#asp.netquery-string

提问by Robin Day

I need to redirect to a url passing a parameter as a query string.

我需要重定向到将参数作为查询字符串传递的 url。

This can include an Ampersand in the value. such as

这可以在值中包含与号。如

string value = "This & That";
Response.Redirect("http://www.example.com/?Value=" + Server.UrlEncode(value));

This however returns http://www.example.com/?Value=This+&+That

然而,这会返回http://www.example.com/?Value=This+&+That

What should I be using to encode this string?

我应该用什么来编码这个字符串?

EDIT: Thanks Luke for pointing out the obvious, the code does indeed work correctly. I Apologise, my question was not a valid question after all!

编辑:感谢卢克指出明显的,代码确实工作正常。我道歉,我的问题毕竟不是一个有效的问题!

The page I was going to had a lot of old legacy code which is apparently doing some kinda of encoding and decoding itself making it appear as if my urlencode was not working.

我将有很多旧的遗留代码的页面,这些代码显然在进行某种编码和解码,使其看起来好像我的 urlencode 不起作用。

My solution unfortunately is to completely drop use of an & until the code in question can be re-written. Don't you just hate old code!

不幸的是,我的解决方案是完全放弃使用 & 直到可以重写有问题的代码。难道你只是讨厌旧代码!

采纳答案by LukeH

The documentationsuggests that Server.UrlEncodeshould handle ampersands correctly.

文件建议,Server.UrlEncode要正确处理好与符号。

I've just tested your exact code and the returned string was correctly encoded:

我刚刚测试了您的确切代码并且返回的字符串已正确编码:

http://www.example.com/?Value=This+%26+That

http://www.example.com/?Value=This+%26+That

回答by Dillie-O

Technically doing:

技术上做:

value = value.Replace("&", "%26") 

will do the trick.

会做的伎俩。

EDIT: There seem to be some tricky issues with the whole UrlEncode/HttpEncode methods that don't quite do the trick. I wrote up a simple method a while back that may come in handy. This should cover all the major encoding issues, and its easy to write a "desanitizer" as well.

编辑:整个 UrlEncode/HttpEncode 方法似乎存在一些棘手的问题,它们并不能完全解决问题。不久前我写了一个简单的方法,可能会派上用场。这应该涵盖所有主要的编码问题,并且也很容易编写“消毒剂”。

Protected Function SanitizeURLString(ByVal RawURLParameter As String) As String

      Dim Results As String

      Results = RawURLParameter    

      Results = Results.Replace("%", "%25")
      Results = Results.Replace("<", "%3C")
      Results = Results.Replace(">", "%3E")
      Results = Results.Replace("#", "%23")
      Results = Results.Replace("{", "%7B")
      Results = Results.Replace("}", "%7D")
      Results = Results.Replace("|", "%7C")
      Results = Results.Replace("\", "%5C")
      Results = Results.Replace("^", "%5E")
      Results = Results.Replace("~", "%7E")
      Results = Results.Replace("[", "%5B")
      Results = Results.Replace("]", "%5D")
      Results = Results.Replace("`", "%60")
      Results = Results.Replace(";", "%3B")
      Results = Results.Replace("/", "%2F")
      Results = Results.Replace("?", "%3F")
      Results = Results.Replace(":", "%3A")
      Results = Results.Replace("@", "%40")
      Results = Results.Replace("=", "%3D")
      Results = Results.Replace("&", "%26")
      Results = Results.Replace("$", "%24")

      Return Results

End Function

回答by goku_da_master

Another character that needs escaping is the apostrophe. Replace it with %27.

另一个需要转义的字符是撇号。将其替换为 %27。

string url = Server.UrlEncode(value).Replace("'", "%27);

string url = Server.UrlEncode(value).Replace("'", "%27);

HttpUtility.UrlEncode() And Server.UrlEncode() do not replace this character along with a few others for backwards compatibility with other .Net Frameworks. See this microsoft article for details: http://connect.microsoft.com/VisualStudio/feedback/details/214349/httputility-urlencode-does-not-encode-apostrophe

HttpUtility.UrlEncode() 和 Server.UrlEncode() 不会与其他一些字符一起替换此字符,以便与其他 .Net 框架向后兼容。有关详细信息,请参阅这篇微软文章:http: //connect.microsoft.com/VisualStudio/feedback/details/214349/httputility-urlencode-does-not-encode-apostrophe

回答by tigerdev

this is correct however if you have several parameters in the query string.

但是,如果查询字符串中有多个参数,这是正确的。

for example : &firstname=bob&secondName="Tracy and John"

例如:&firstname=bob&secondName="Tracy and John"

回答by Kinch

And if you are getting a value from a GridViewthe & ampersandmay very well be showing up as "&amp;":

如果你是从得到一个值GridView& ampersand很可能被显示为"&amp;"

row.Cells[4].Text.ToString() = xxxx&amp;

So in this case you will want to use:

因此,在这种情况下,您将需要使用:

.Replace("&amp;", "%26")

回答by Omgee Cares

You must use Server.UrlEncode(string containing the ampersand).

您必须使用Server.UrlEncode(string containing the ampersand).

I've just tested it and the returned query string was correctly encoded and then decoded.

我刚刚测试了它,返回的查询字符串被正确编码然后解码。

HttpUtilitydidn't work for this operation.

HttpUtility不适用于此操作。

回答by Eli

In .net core ver 2.1 ( late 2018 ) I was able to use the following:

在 .net core ver 2.1(2018 年末)中,我能够使用以下内容:

System.Web.HttpUtility.UrlEncode(string_to_encode)