2009-09-17 57 views
0

C#,ASP.NET 3.5请求对象不解码了urlencoded

创建简单的URL与编码查询字符串:

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

成为很好:http://localhost/test.aspx?a=%E1%ED%F3%FA(即良好)

当我调试test.aspx我得到奇怪的解码:

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

为什么会曲eryString不解码值?

+0

会发生什么事,当你做字符串badDecode =的Request.QueryString [ “一个”]。的ToString();? ToString()是语言环境特定的线程,有时可以做魔术。 – 2009-09-17 23:28:42

回答

1

你可以尝试使用HttpServerUtility.UrlEncode代替。在Microsoft.JScript.GlobalObject.escape状态,它不是inteded

微软文档,直接从您的代码中使用。

编辑:
正如我在评论中说:这两种方法不同的编码和预计的Request.QueryString通过HttpServerUtility.UrlEncode使用的编码,因为它内部使用HttpUtility.UrlDecode。

(HttpServerUtility.UrlEncode实际使用HttpUtility.UrlEncode内部。)

你可以很容易地看到这两种方法之间的差异。
创建一个新的ASP.NET Web应用程序,添加到Microsoft.JScript程序的引用,然后添加以下代码:

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"]); 
    } 
} 

结果应该是:

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

���� 
áíóú 
+0

问题不在于编码器,这是因为接收页面中的QueryString部分乱码。 – 2009-09-17 19:24:53

+0

我尝试了Microsoft.JScript.GlobalObject.escape和HttpServerUtility.UrlEncode,前者给出乱码,后者工作正常。 – 2009-09-18 05:34:56

+0

这些方法编码不同。 QueryString需要HttpServerUtility.UrlEncode使用的格式。 – 2009-09-18 05:44:19