简体   繁体   English

Request.Querystring自动url解码字符串?

[英]does Request.Querystring automatically url decode a string?

I'm working with a page where I have a url like: 我正在使用我有一个网址的页面:
/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 . 所以我想我可以用%26替换&符号。 However, when I debug the code and hover over Request.QueryString it shows me {qq=company&manf=dunkin&donuts&cond=} and Request.QueryString["manf"] gives me 'dunkin' 但是,当我调试代码并将鼠标悬停在Request.QueryString它显示{qq=company&manf=dunkin&donuts&cond=}Request.QueryString["manf"]给了我'dunkin'

If I use %24 ($) instead of ampersand, hovering over Request.QueryString gives me {qs=company&manf=dunkin%24donuts&cond=} and Request.QueryString["manf"] gives me 'dunkin$donuts' 如果我使用%24 ($)而不是&符号,将{qs=company&manf=dunkin%24donuts&cond=}悬停在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. 我一直认为Request.QueryString[key]返回实际的文本而不先解码它。 Or does it have something to do with url re-writes? 或者它与网址重写有什么关系?

ASP.NET automatically calls UrlDecode() when you access a property by key index (ie ( Request.QueryString["key"] ). 当您通过键索引访问属性时,ASP.NET会自动调用UrlDecode() (即( Request.QueryString["key"] )。

If you want it encoded, just do: 如果你想要它编码,只需:

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编码和解码&符号应该总是给你&出于这个原因。

Replacing the ampersand with %26 should cause that value to be escaped, so Request.QueryString["manf"] would yield dunkin&donuts . %26替换&符应该导致该值被转义,因此Request.QueryString["manf"]将产生dunkin&donuts

The asker of this similar question ended 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 %26 into an ampersand before sending it to your server. 也许有些javascript将%26解码为&符号,然后再将其发送到您的服务器。 Try using Firebug or Chrome's developer tools to see the actual URL string that is being sent from the browser. 尝试使用Firebug或Chrome的开发人员工具查看从浏览器发送的实际URL字符串。

Update 更新

After looking at the question again, I realize that you're probably using a URL Rewriter. 在再次查看问题后,我意识到您可能正在使用URL重写器。 This post describes a similar problem, and I don't know of a solution for sure, but you may want to try double-encoding the ampersand using %2526 instead of %26 . 这篇文章描述了一个类似的问题,我不确定解决方案,但您可能想尝试使用%2526而不是%26对&符号进行双重编码。

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}}. 这里重要的一行是{UrlEncode:{R:2}}。 It solved the problem for me! 它解决了我的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM