C#,ASP.NET 3.5
我用一个编码的查询字符串创建了一个简单的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
为什么QueryString不对这些值进行解码?
发布于 2009-09-17 17:17:09
您可以尝试使用HttpServerUtility.UrlEncode。
微软关于Microsoft.JScript.GlobalObject.escape的文档指出,它不会被集成到代码中直接使用。
编辑:
正如我在注释中所说的:这两种方法的编码方式不同,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
����
áíóú
https://stackoverflow.com/questions/1439990
复制相似问题