ASP.Net MVC 2.0 : unescaping URL query parameters
-
09-10-2019 - |
Question
I have following URL:
http://localhost:8041/Reforge.aspx?name=Cyan%ECde&realmId=1
notice %EC
in the value of name
parameter.
%EC = 236 = ì (igrave)
In my action method:
public ActionResult Index(string name, int realmId) {...}
name[4] is a character with code 65533 (0xFFFD). What am I doing wrong?
Solution
This will depend on the globalization element in web.config:
<globalization requestEncoding="iso-8859-1" />
Or if your site is UTF-8
<globalization requestEncoding="utf-8" />
But in this case the url needs to look like name=Cyan%c3%acde
. You should always use URL helpers to generate urls so that they are properly encoded.
OTHER TIPS
HttpUtility.UrlDecode("%EC")
gives that character (65533) as its output.
HttpUtility.UrlEncode("ì")
produces "%c3%ac"
How are you generating this %EC
? It looks like your encoding isn't working as ASP.NET is expecting
UPDATE
You say that you're just entering http://localhost:8041/Reforge.aspx?name=Cyanìde&realmId=1
into your browser and that it's not encoding correctly. I would suggest that you shouldn't be entering that into your browser in the first place. If you're generating that URL, you need to encode it (so that it renders as <a href="http://localhost:8041/Reforge.aspx?name=Cyan%c3%acde&realmId=1">
). Firefox will show this with the ì when hovering over, but will give the encoded version when clicked or copied.
If users are typing arbitrary unicode into a URL, there's not a good way for you to handle that (since they're effectually sending you invalid requests).
Some things I can find:
HttpServerUtility.UrlDecode(your param)
HttpUtility.UrlDecode(your param)
Server.UrlDecode(your param)
Any of these working for you?
I faced a similar problem with url encoding (ì must be encoded as %EC to work) in ASP.NET and js.
If you set <globalization requestEncoding="ISO-8859-1" responseEncoding="ISO-8859-1" \>
in system.web
node, you must use HttpContext.Current.Server.UrlEncode
to urlencode \ decode cause in this way you are using globalization settings in web.config
.
Before using UrlEncode
in current HttpContext
I was using HttpUtility.UrlDecode
that doesn't use current globalization settings.
Worked for me!