views:

474

answers:

1

C#, ASP.NET 3.5

I create a simple URL with an encoded querystring:

string url = "http://localhost/test.aspx?a=" +
     Microsoft.JScript.GlobalObject.escape("áíóú");

which becomes nicely: http://localhost/test.aspx?a=%E1%ED%F3%FA (that is good)

When I debug test.aspx I get strange decoding:

string badDecode = Request.QueryString["a"];  //bad
string goodDecode = Request.Url.ToString();    //good

Why would the QueryString not decode the values?

+1  A: 

You could try to use HttpServerUtility.UrlEncode instead.

Microsoft documentation on Microsoft.JScript.GlobalObject.escape states that it isn't inteded to be used directly from within your code.

Edit:
As I said in the comments: The two methods encode differently and Request.QueryString expects the encoding used by HttpServerUtility.UrlEncode since it internally uses HttpUtility.UrlDecode.

(HttpServerUtility.UrlEncode actually uses HttpUtility.UrlEncode internally.)

You can easily see the difference between the two methods.
Create a new ASP.NET Web Application, add a reference to Microsoft.JScript then add the following code:

protected void Page_Load(object sender, EventArgs e)
{
  var msEncode = Microsoft.JScript.GlobalObject.escape("áíóú");
  var httpEncode = Server.UrlEncode("áíóú");

  if (Request.QueryString["a"] == null)
  {
    var url = "/default.aspx?a=" + msEncode + "&b=" + httpEncode;
    Response.Redirect(url);
  }
  else
  {
    Response.Write(msEncode + "<br />");
    Response.Write(httpEncode + "<br /><br />");

    Response.Write(Request.QueryString["a"] + "<br />");
    Response.Write(Request.QueryString["b"]);
  }
}

The result should be:

%E1%ED%F3%FA
%c3%a1%c3%ad%c3%b3%c3%ba

����
áíóú
Sani Huttunen
The problem is not with the encoder, it's with the fact that the QueryString in the receiving page gets partial gibberish.
BahaiResearch.com
I tried both Microsoft.JScript.GlobalObject.escape and HttpServerUtility.UrlEncode and the former gives gibberish and the latter works fine.
Sani Huttunen
The methods encode differently. QueryString expects the format which HttpServerUtility.UrlEncode uses.
Sani Huttunen