Ampersands in URLRewriter Query Strings

烈酒焚心 提交于 2019-11-30 18:09:27

问题


I have a query string parameter value that contains an ampersand. For example, a valid value for the parameter may be:

a & b

When I generate the URL that contains the parameter, I'm using System.Web.HTTPUtility.UrlEncode() to make each element URL-friendly. It's (correctly) giving me a URL like:

http://example.com/foo?bar=a+%26b

The problem is that ASP.NET's Request object is interpreting the (encoded) ampersand as a Query String parameter delimiter, and is thus splitting my value into 2 parts (the first has "bar" as the parameter name; the second has a null name).

It appears that ASP.NET is URL-decoding the URL first and then using that when parsing the query string.

What's the best way to work around this?


UPDATE: The problem hinges on URLRewriter (a third-party plugin) and not ASP.NET itself. I've changed the title to reflect this, but I'll leave the rest of the question text as-is until I find out more about the problem.


回答1:


man, i am with you in the same boat, i have spent like hours and hours trying to figure out what is the problem, and as you said it is a bug in both, as normal links that contain weird characters or UTF-8 code characters are parsed fine by asp.net.

i think we have to switch to MVC.routing

Update: man you wont believe it, i have found the problem it is so strange, it is with IIS, try to launch your page from visual studio Dev server and Unicode characters will be parsed just fine, but if you launch the page from IIS 7 it will give you the ???? characters.

hope some body will shade some light here




回答2:


I would have thought that %26 and '&' mean exactly the same thing to the web server, so its the expected behavior. Urlencode is for encoding URLs, not encoding query strings.

... hang on ... Try searching for abc&def in google, you'll get:

http://www.google.com.au/search?q=abc%26def

So your query string is correct, %26 is a literal ampersand. Hmm you're right, sounds like a bug. How do you go with an & instead of the %26 ?

Interesting reading:

http://www.stylusstudio.com/xsllist/200104/post11060.html




回答3:


Switching to UrlRewritingNet.UrlRewrite did not help, as it apparently has the same bug. I'm thinking it might have something to do with ASP.NET after all.




回答4:


I think URLRewriter has a problem with nameless parameters (null name).
I had a similar problem. When I gave my nameless parameter a (dummy) name, everything worked as expected.



来源:https://stackoverflow.com/questions/529857/ampersands-in-urlrewriter-query-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!