加号查询字符串 [英] Plus sign in query string
问题描述
我有一个使用 C# 和 asp.net 创建的 web 应用程序.我在查询字符串中放置了一个带有加号 (+) 的参数值.但是加号消失了.
I have a webapp created using C# and asp.net. I placed a parameter value in the querystring with a plus(+) sign. But the plus sign disappear.
如何在查询字符串中包含加号(+)而不消失?
How can I include the plus sign(+) in the query string without disappearing?
请指教.
谢谢.
使用 UrlEncode 添加代码
added code with UrlEncode
string str = Server.UrlEncode(Requery.QueryString["new"]);
推荐答案
+
符号在查询字符串中具有语义含义.它用于表示一个空间.另一个在查询字符串中具有语义重要性的字符是 &
,它用于分隔查询字符串中的各种 var=value
对.
+
sign has a semantic meaning in the query string. It is used to represent a space. Another character that has semantic importance in the query string is &
which is used to separate the various var=value
pairs in the query string.
大多数服务器端脚本会在使用查询参数之前对其进行解码,以便 +
正确转换为空格.现在,如果您希望在查询字符串中出现文字 +
,则需要改为指定 %2B
.
Most server side scripts would decode the query parameters before using them, so that a +
gets properly converted to a space. Now, if you want a literal +
to be present in the query string, you need to specify %2B
instead.
+
符号被 URL 解码为一个空格.查询字符串中的 %2B
被 URL 解码为 + 号.
+
sign in the query string is URL-decoded to a space. %2B
in the query string is URL-decoded to a + sign.
看两者的区别
http://www.google.com/search?q=foo+bar
和
http://www.google.com/search?q=foo%2Bbar
在上述示例中,Google 的服务器脚本对查询参数进行 URL 解码,然后使用它们进行搜索.
In the above examples, Google's server script is URL-decoding the query parameters and then using them to do the search.
URL 编码只不过是 %
符号后跟特殊字符的十六进制代码.例如,我们知道A
的十六进制代码是0x41(十进制:65).试试这个:
URL-encoding is nothing but %
sign followed by the hex-code of the special character. For example, we know that the hex code of A
is 0x41 (decimal: 65). Try this:
http://www.google.com/search?q=%41
希望这能让 URL 编码变得清晰.
Hope this makes URL-encoding clear.
因此,如果您希望在 JavaScript 获取查询参数中带有 +
符号的 URL 时保留 +
符号,并且服务器端脚本将处理URL解码后的查询参数,您应该在使用发出HTTP get请求之前对URL中的查询参数进行URL编码,以便将所有+
符号转换为%2B
's 当请求到达服务器端脚本时.现在,当服务器端脚本对查询字符串进行 URL 解码时,所有 %2B
都会转换回 +
符号,这正是您想要的.
So, if you want the +
sign to be preserved when a JavaScript is fetching a URL with +
signs in its query parameters and a server side script would process the query parameters after URL-decoding it, you should URL-encode the query parameters in the URL before using issuing the HTTP get request so that all +
signs are converted to %2B
's when the request reaches the server side script. Now when the server side script URL-decodes the query string, all %2B
's gets converted back to +
signs which is what you want.
请参阅在 JavaScript 中编码 URL?,了解如何使用 JavaScript 对参数进行 URL 编码.那里讨论的简短回答:
See Encode URL in JavaScript? to learn how to URL-encode the parameters using JavaScript. Short answer from the discussion there:
var encodedURL = "http://example.com/foo.php?var=" + encodeURIComponent(param);
这篇关于加号查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!