zoukankan      html  css  js  c++  java
  • asp.net(C#)中文乱码问题

    asp.net默认的编码是UTF-8
    js文件里的编码也是UTF-8
    当你要在aspx页面上进行传中文参数时会出现乱码
    <-----request.aspx--接收参数页----->
    <----response.aspx--传送参数页----->

    例一:<a href="request.aspx?str=中国人"></a>

    解决办法一:
    1.可以和改webconfig的编码 如:
            <location path='response.aspx'>
                <system.web>
                    <globalization fileEncoding='gb2312' requestEncoding='gb2312' responseEncoding='gb2312' culture='zh-CN'/>
                </system.web>
            </location>
    注意:你也要把request.aspx页面上的编码也改成同样的,虽然中文乱码解决了,但如果你用到了js文件就会出现乱码
    //用这以上方法的话不会改变网站的其它页面上的编码
            <location path='request.aspx'>
                <system.web>
                    <globalization fileEncoding='gb2312' requestEncoding='gb2312' responseEncoding='gb2312' culture='zh-CN'/>
                </system.web>
            </location>
    解决办法二:
    1.如果你不想动webconfig 你可以在”response.aspx.cs“里面对参数进行编码 如:
    response.aspx在页面上:
    <a href="request.aspx?str=<%=str%>"></a>
    response.cs页面上:
             声明一个变量str
            public string str="中国人";
            str= HttpUtility.UrlEncode(str,System.Text.Encoding.GetEncoding("GB2312"));
    //这时str已经是编码后的

    2.而在request.aspx.cs文件中也要进行转换 如:
    声明一个变量   System.Collections.Specialized.NamueCollection gb2312=HttpUtility.ParseQueryString(Request.Url.Query,System.Text.Encoding.GetEncoding("GB2312"));
    string str=gb2312["str"];
    这里的str就是你要接收的中文。

    例二
    如果你想在js里面传送中文参数 如:
    request.aspx?str="+encodeURI("中国人");
    这样就不会出现乱码了

    例三
    就是我在cshn上找到的一个方法我也没试过,大家可以试一下
    protected   string   GetQueryString(string   sKey,System.Text.Encoding   e)
    {string QueryString=Server.UrlDecode(System.Web.HttpUtility.UrlDecode(Request.ServerVariables[ "QUERY_STRING "],e));
    System.Text.Regularexpression_rs.Regex   reg    new   System.Text.Regularexpression_rs.Regex(sKey+"=([^&$]*?)(&|$) ");
    System.Text.Regularexpression_rs.Match    reg.Match(QueryString); 
        if   (m.Success)    return   m.Result( "$1 ");  }
             else   return   String.Empty;              
    }
    //以上这个不受编码影响,只需知道原来传入的编码就可.

    技术改变世界
  • 相关阅读:
    ajax
    Django之modelform组件
    Django之form组件
    orm事务与锁
    orm之多表操作
    orm之单表操作
    Django之orm
    Django之模板系统
    Django之视图
    hdu5698瞬间移动(杨辉三角+快速幂+逆元)
  • 原文地址:https://www.cnblogs.com/davidgu/p/2234450.html
Copyright © 2011-2022 走看看