does Request.Querystring automatically url decode a string?

前端 未结 3 2062
隐瞒了意图╮
隐瞒了意图╮ 2020-12-06 04:26

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/

相关标签:
3条回答
  • 2020-12-06 04:54

    ASP.NET automatically calls UrlDecode() when you access a property by key index (i.e. (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.

    0 讨论(0)
  • 2020-12-06 05:05

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

        <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!

    0 讨论(0)
  • 2020-12-06 05:10

    Replacing the ampersand with %26 should cause that value to be escaped, so Request.QueryString["manf"] would yield 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. Try using Firebug or Chrome's developer tools to see the actual URL string that is being sent from the browser.

    Update

    After looking at the question again, I realize that you're probably using a URL Rewriter. 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.

    0 讨论(0)
提交回复
热议问题