C# Request.Querystring 会自动对字符串进行 url 解码吗?

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

does Request.Querystring automatically url decode a string?

c#asp.netc#-4.0urlurl-rewriting

提问by merk

I'm working with a page where I have a url like:
/directory/company/manufacturer

我正在使用一个页面,其中有一个 url,如:
/directory/company/manufacturer

Using some re-write rules this gets re-written

使用一些重写规则,这会被重写

testing with /directory/company/dunkin%26donuts/

使用 /directory/company/dunkin%26donuts/ 进行测试

Some manufacturers have an ampersand in their name. So I thought I could just replace the ampersand with %26. However, when I debug the code and hover over Request.QueryStringit shows me {qq=company&manf=dunkin&donuts&cond=}and Request.QueryString["manf"]gives me 'dunkin'

一些制造商在他们的名字中有一个符号。所以我想我可以用%26. 但是,当我调试代码并将鼠标悬停在Request.QueryString它上面时,它会显示我{qq=company&manf=dunkin&donuts&cond=}Request.QueryString["manf"]给我 'dunkin'

If I use %24($) instead of ampersand, hovering over Request.QueryStringgives me {qs=company&manf=dunkin%24donuts&cond=}and Request.QueryString["manf"]gives me 'dunkin$donuts'

如果我使用%24($) 而不是 & 符号,悬停在上面Request.QueryString会给我 {qs=company&manf=dunkin%24donuts&cond=}Request.QueryString["manf"]给我 'dunkin$donuts'

I don't understand the different behavior here. Why does it seem as though the url-encoded value for an ampersand gets decoded before you actually request a specific key, but another url-encoded character, like a dollar sign, only gets decoded after you actually request that specific key?

我不明白这里的不同行为。为什么看起来好像在您实际请求特定密钥之前对&符号的 url 编码值已被解码,但另一个 url 编码字符(如美元符号)仅在您实际请求该特定密钥后才被解码?

Is this a recent change? I always thought Request.QueryString[key]returned the actual text without decoding it first. Or does it have something to do with url re-writes?

这是最近的变化吗?我一直认为Request.QueryString[key]返回实际文本而不先解码它。或者它与url重写有关吗?

采纳答案by StriplingWarrior

Replacing the ampersand with %26should cause that value to be escaped, so Request.QueryString["manf"]would yield dunkin&donuts.

替换 & 号%26应该会导致该值被转义,因此Request.QueryString["manf"]yield dunkin&donuts

The asker of this similar questionended up realizing that some other code on the same page ended up pre-decoding his ampersands. Is it possible that you've got something similar happening? Perhaps some javascript is decoding the %26into an ampersand before sending it to your server. Try using Firebug or Chrome's developer tools to see the actual URL string that is being sent from the browser.

这个类似问题的提问者最终意识到同一页面上的其他一些代码最终预解码了他的&符号。你有没有可能发生过类似的事情?也许某些 javascript%26在将它发送到您的服务器之前将其解码为一个&符号。尝试使用 Firebug 或 Chrome 的开发人员工具查看从浏览器发送的实际 URL 字符串。

Update

更新

After looking at the question again, I realize that you're probably using a URL Rewriter. This postdescribes a similar problem, and I don't know of a solution for sure, but you may want to try double-encoding the ampersand using %2526instead of %26.

再次查看问题后,我意识到您可能正在使用 URL 重写器。这篇文章描述了一个类似的问题,我不知道肯定的解决方案,但你可能想尝试双重编码使用符号%2526代替%26

回答by mattytommo

ASP.NET automatically calls UrlDecode()when you access a property by key index (i.e. (Request.QueryString["key"]).

UrlDecode()当您通过键索引(即 ( Request.QueryString["key"])访问属性)时,ASP.NET 会自动调用。

If you want it encoded, just do:

如果您想对其进行编码,只需执行以下操作:

HttpUtility.UrlEncode(Request.QueryString["key"]);

HttpUtility.UrlEncode(Request.QueryString["key"]);

In terms of the ampersand specifically, that is a special case character because it is already used as a query string delimeter. URL Encoding and decoding an ampersand should always give you &for that very reason.

具体而言,就与号而言,这是一个特例字符,因为它已被用作查询字符串分隔符。&出于这个原因,URL 编码和解码一个&符号应该总是给你。

回答by Olaj

I think a solution might be to modify the UrlRewrite rule to something like.

我认为解决方案可能是将 UrlRewrite 规则修改为类似的内容。

    <rule name="TagPage" stopProcessing="true">
      <match url="^(tag)/([^/]+)/?$"/>
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
      </conditions>
      <action type="Rewrite" url="ListByTags.aspx?tag={UrlEncode:{R:2}}"/>
    </rule>

The important line here is the {UrlEncode:{R:2}}. It solved the problem for me!

这里重要的一行是 {UrlEncode:{R:2}}。它为我解决了问题!