网络编程
位置:首页>> 网络编程>> Asp编程>> ASP编码问题的深入研究与解决方案(MSDN)(2)

ASP编码问题的深入研究与解决方案(MSDN)(2)

  发布时间:2007-10-25 11:54:00 

标签:编码,CODEPAGE,Charset,MSDN

这句话进一步解释了产生乱码的原因

If you set Response.CodePage or Session.CodePage explicitly, do so before sending non-literal strings to the client. If you use literal and non-literal strings in the same page, make sure the code page of @CODEPAGE matches the code page of Response.CodePage, or the literal strings are encoded differently from the non-literal strings and display incorrectly. 

其中比较有用的一句话是说如果Response.CodePage和@CODEPAGE不一样的话会产生乱码。也就是说当Z-blog的@CODEPAGE=65001而Z-blog的Response.CodePage被Session.CodePage赋为936的时候就会出现乱码,oblog反之亦然。

不知道上面说了这么多解释清楚没有-_-

下面解释一下为什么SXNA有时会把Session.CodePage赋为936,我有一个版本是这样写的:


<% OriginalCodePage=Session.CodePage %>
.......
<% Session.CodePage=OriginalCodePage %>

 


当用户进入浏览器的时候Session.CodePage默认为936,这个时候的默认936不是程序声明的,因此不会赋给Response.CodePage,当进入SXNA的时候,Session.CodePage被上面那段代码一折腾就变成了程序声明的Session.CodePage=936,因此再进入Zblog的时候就把936给了Response.CodePage。

至此,全部原因已经分析清楚了。

因此说,保证asp页面一定不会出现乱码的代码应该是这样的:(假定是UTF-8的页面)


<%@ CODEPAGE=65001 %>
<% Response.CodePage=65001%>
<% Response.Charset="UTF-8" %>


进一步说明为什么要加Response.Charset,因为MSDN说应该加...呵呵

If the code page is set in a page, then Response.Charset should also be set.
 

另外,文件的编码格式应该与@CODEPAGE一样:

The file format of a Web page must be the same as the @CODEPAGE used in the page.

这就是为什么zblog,pjblog等一些程序要吧文件存成UTF8编码格式的原因.

综上,如果所有的程序都声明了Response.CodePage就不会被Session.CodePage干扰而出现乱码了。所以Session.CodePage还是不能轻易用的!

参考文章:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/268f1db1-9a36-4591-956b-d7269aeadcb0.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/582e6f47-52eb-413e-8b5d-c99145cb61d8.asp

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com