<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編碼,除非有特殊用途。聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com