欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

静态gb2312编码在项目传值出现中文乱码现象

程序员文章站 2024-02-29 17:39:34
可能标题描述的不是特别清楚 当时的问题是这样的:在我的系统项目中,参考的美工静态页面是gb2312格式的,当此编码拿到项目中后,utf-8编码的系统,加载页面时,会出现样式...
可能标题描述的不是特别清楚
当时的问题是这样的:在我的系统项目中,参考的美工静态页面是gb2312格式的,当此编码拿到项目中后,utf-8编码的系统,加载页面时,会出现样式问题,比如不能正常居中等。(ie6通常有样式问题)
解决办法如下,(如果必须采用gb2312编码的话)
在webconfig的<system.web>中增加
复制代码 代码如下:

<globalization requestencoding="gb2312" responseencoding="gb2312" uiculture="zh-cn" culture="zh-cn" fileencoding="gb2312"/>

此时,页面效果正常。
但是,如果这个时候遇到有页面传值中文的功能时,传值的中文会乱码。即使在js中用了encodeuricomponent也不能解决
此时,在项目中增加如下类,用于专门处理request传值转回utf-8格式。
复制代码 代码如下:

namespace xxx
{
//用于处理ie6下utf-8得不到样式的问题:将web的编码改为gb2312,request传值通过本方法转回utf-8
public class contentencodingmodule : ihttpmodule
{
public void init(httpapplication app)
{
app.beginrequest += new eventhandler(app_beginrequest);
}
public void dispose()
{
}
void app_beginrequest(object sender, eventargs e)
{
httpapplication app = (httpapplication)sender;
httpworkerrequest request = (((iserviceprovider)app.context)
.getservice(typeof(httpworkerrequest)) as httpworkerrequest);
app.request.contentencoding = system.text.encoding.utf8;
}
}
}

并在webconfig中引用此类
复制代码 代码如下:

<httpmodules>
<add name="contentencodingmodule" type="xxx.contentencodingmodule,xxx"/>
</httpmodules>

问题解决。
但根据参考文章说,尽量还是不要将静态页用gb2312编码,除非有特殊用途。