Asp.net通用处理程序编码问题

时间:2010-09-06 18:22:59

标签: asp.net ajax character-encoding

我的问题不是太难,但真的很烦人。

我正在通过这种方式通过Ajax向Generic Handler发送值。

xmlHttpReq.open("GET", "AddMessage.ashx?" + (new Date().getTime()) +"&Message=" + Message,true);

当消息包含İ,ç,ö,ğ,ü,ı他们在Handler上看起来像那样 在context.Request.RawURLİ,ç,ö,ğ,ü,ı这些字符看起来应该如此。但是在context.Request.Url中它们看起来像 ,当我想要QueryString值时,它给了我 我能做什么?

2 个答案:

答案 0 :(得分:2)

要检查的几件事情:

  1. web.config中,您设置了UTF-8:

    <system.web>
        <globalization requestEncoding="utf-8" responseEncoding="utf-8" />
        ...
    </system.web>
    
  2. 您的HTML网页中有一个正确的元标记:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
  3. 您的所有.aspx.ascx.master.ashx,...文件都将保存为UTF-8,其中包含硬盘驱动器上的BOM。< / p>

  4. 在发送参数之前,您是正确的URL编码参数(使用encodeURIComponent方法):

    xmlHttpReq.open(
        "GET", 
        "AddMessage.ashx?" + 
            (new Date().getTime()) + 
            "&Message=" + encodeURIComponent(Message),
        true
    );
    

答案 1 :(得分:0)

我在这个链接上找到了答案。 ASP.NET & Ajax: query string parameters using ISO-8859-1 encoding

// original parameterized value with invalid characters
string paramQs = context.Request.QueryString["param"];
// correct parsed value from query string parameter
string param = Encoding.UTF8.GetString(Encoding.GetEncoding("iso8859-1").GetBytes(paramQs));
相关问题