可能標(biāo)題描述的不是特別清楚
當(dāng)時的問題是這樣的:在我的系統(tǒng)項目中,參考的美工靜態(tài)頁面是gb2312格式的,當(dāng)此編碼拿到項目中后,utf-8編碼的系統(tǒng),加載頁面時,會出現(xiàn)樣式問題,比如不能正常居中等。(IE6通常有樣式問題)
解決辦法如下,(如果必須采用gb2312編碼的話)
在webconfig的system.web>中增加
復(fù)制代碼 代碼如下:
globalization requestEncoding="gb2312" responseEncoding="gb2312" uiCulture="zh-CN" culture="zh-CN" fileEncoding="gb2312"/>
此時,頁面效果正常。
但是,如果這個時候遇到有頁面?zhèn)髦抵形牡墓δ軙r,傳值的中文會亂碼。即使在js中用了encodeURIComponent也不能解決
此時,在項目中增加如下類,用于專門處理request傳值轉(zhuǎn)回utf-8格式。
復(fù)制代碼 代碼如下:
namespace XXX
{
//用于處理IE6下UTF-8得不到樣式的問題:將web的編碼改為gb2312,request傳值通過本方法轉(zhuǎn)回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中引用此類
復(fù)制代碼 代碼如下:
httpModules>
add name="ContentEncodingModule" type="XXX.ContentEncodingModule,XXX"/>
/httpModules>
問題解決。
但根據(jù)參考文章說,盡量還是不要將靜態(tài)頁用gb2312編碼,除非有特殊用途。
您可能感興趣的文章:- UTF8轉(zhuǎn)成GB2312亂碼問題解決方案
- Utf-8和Gb2312亂碼問題的終結(jié)
- 解決Jquery load()加載GB2312頁面時出現(xiàn)亂碼的兩種方案