asp.net 頁面編碼常見問題小結(jié)
更新時間:2010年06月03日 12:53:49 作者:
asp.net 頁面編碼常見問題小結(jié)
如果要為整個項目設(shè)置頁面編碼,那么就可以在 Web.config 文件中添加一個 Globalization 屬性,然后設(shè)置它的 fileEncoding、requestEncoding,和 responseEncoding 特性:
<configuration>
<system.web>
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-US"
uiCulture="de-DE"
/>
</system.web>
</configuration>
如果要為單獨(dú)的頁面設(shè)置編碼,那么就可以設(shè)置 @ Page 指令的 RequestEncoding 和 ResponseEncoding 特性:
<%@ Page RequestEncoding="utf-8" ResponseEncoding="utf-8" %>
有時我們已經(jīng)在配置文件中將整個站點(diǎn)的編碼設(shè)置為gb2312,但某個頁面卻需要使用utf-8,這時我們可以在配置文件configuration節(jié)下新增location節(jié)點(diǎn):
程序代碼
<location path="Test.aspx">
<system.web>
<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="en"/>
</system.web>
</location>
如果是要將某個頁面單獨(dú)設(shè)置為gb2312則為:
程序代碼<location path="Test.aspx">
<system.web>
<globalization fileEncoding="gb2312" requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN"/>
</system.web>
</location>
以下是一些網(wǎng)友的解決問題的方法參考:
在用ASP.NET寫網(wǎng)上支付的接口程序時,遇到一個奇怪問題,通過表單提交過去的中文全是亂碼,英文正常。而用asp程序進(jìn)行測試,可以正常提交中文,asp頁面中有這樣的HTML代碼:
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" >
可是將這個代碼加入到ASP.NET頁面中,依然解決不了問題。分析了一下,問題應(yīng)該是編碼引起的,對方的程序只能處理GB2312編碼的頁面提交過來的中文數(shù)據(jù)。難道加了上面的代碼,ASP.NET卻不是以GB2312編碼顯示的?打開該頁面,查看一下瀏覽器的編碼,原來是UTF-8,原因找到,怎么解決呢?看來,ASP.NET不理睬上面的代碼,自己向瀏覽器發(fā)送編碼信息,那我設(shè)置一下Response.ContentEncoding試試,在Page_Load中加上如下代碼:
Response.ContentEncoding = System.Text.Encoding.GetEncoding( " GB2312 " );
OK!問題解決!
<configuration>
<system.web>
<globalization
fileEncoding="utf-8"
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-US"
uiCulture="de-DE"
/>
</system.web>
</configuration>
如果要為單獨(dú)的頁面設(shè)置編碼,那么就可以設(shè)置 @ Page 指令的 RequestEncoding 和 ResponseEncoding 特性:
<%@ Page RequestEncoding="utf-8" ResponseEncoding="utf-8" %>
有時我們已經(jīng)在配置文件中將整個站點(diǎn)的編碼設(shè)置為gb2312,但某個頁面卻需要使用utf-8,這時我們可以在配置文件configuration節(jié)下新增location節(jié)點(diǎn):
程序代碼
<location path="Test.aspx">
<system.web>
<globalization fileEncoding="utf-8" requestEncoding="utf-8" responseEncoding="utf-8" culture="en"/>
</system.web>
</location>
如果是要將某個頁面單獨(dú)設(shè)置為gb2312則為:
程序代碼<location path="Test.aspx">
<system.web>
<globalization fileEncoding="gb2312" requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN"/>
</system.web>
</location>
以下是一些網(wǎng)友的解決問題的方法參考:
在用ASP.NET寫網(wǎng)上支付的接口程序時,遇到一個奇怪問題,通過表單提交過去的中文全是亂碼,英文正常。而用asp程序進(jìn)行測試,可以正常提交中文,asp頁面中有這樣的HTML代碼:
< meta http-equiv ="Content-Type" content ="text/html; charset=gb2312" >
可是將這個代碼加入到ASP.NET頁面中,依然解決不了問題。分析了一下,問題應(yīng)該是編碼引起的,對方的程序只能處理GB2312編碼的頁面提交過來的中文數(shù)據(jù)。難道加了上面的代碼,ASP.NET卻不是以GB2312編碼顯示的?打開該頁面,查看一下瀏覽器的編碼,原來是UTF-8,原因找到,怎么解決呢?看來,ASP.NET不理睬上面的代碼,自己向瀏覽器發(fā)送編碼信息,那我設(shè)置一下Response.ContentEncoding試試,在Page_Load中加上如下代碼:
Response.ContentEncoding = System.Text.Encoding.GetEncoding( " GB2312 " );
OK!問題解決!
相關(guān)文章
Asp.net XML文檔進(jìn)行添加刪改操作的實例代碼
Asp.net 對一下XML文檔進(jìn)行添加刪改的實例2009-12-12動態(tài)ItemTemplate的實現(xiàn)(譯) - item,template
動態(tài)ItemTemplate的實現(xiàn)(譯) - item,template...2007-02-02.NET中可空值類型【Nullable<T>】實現(xiàn)原理
本文主要介紹了.NET中可空值類型的實現(xiàn)原理,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03asp.net中Request.QueryString與Request.Param的區(qū)別分析
看起來Request.Params更好一些,但是還是不明白既然Param包括了所有,為什么還要有QueryString呢2011-10-10ASP.NET?Core使用EF?SQLite對數(shù)據(jù)庫增刪改查
這篇文章介紹了ASP.NET?Core使用EF?SQLite對數(shù)據(jù)庫增刪改查的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01