Coolite在web.config没有配置为UTF8下乱码的解决方法

来源:互联网 发布:嵌入式linux工资 编辑:程序博客网 时间:2024/05/17 07:52

原项目的web.config配置如下:

<globalization requestEncoding="gb2312" responseEncoding="gb2312" culture="zh-CN" uiCulture="en" fileEncoding="gb2312"/>

在该配置下,coolite的显示为乱码,后台取值也为乱码。

显示为乱码可以通过修改页面的ResponseEncoding解决,代码如下。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ItemAuthForm.aspx.cs" Inherits="Study_ItemAuthForm" ResponseEncoding="UTF-8"  Culture="zh-CN"   %>

也可以通过后台代码实现:

 

view plaincopy to clipboardprint?
  1. protected override void OnInit(EventArgs e)  
  2. {  
  3.   
  4.     Page.Response.ContentEncoding = Encoding.UTF8;  
  5.     base.OnInit(e);  
  6. }  
 

 

但取值为乱码的问题却没有办法通过这两种方法解决,原因是Request.ContentEncoding = System.Text.Encoding.UTF8;必须在 BeginRequest, AuthenticateRequest, EndRequest 这些方法之前设置才有效。所以通过修Coolite.Ext.Web.AjaxRequestModule类解决这个问题:

web.config增加配置

 

view plaincopy to clipboardprint?
  1. <httpModules>  
  2.      <!--coolite-->  
  3.       <add name="AjaxRequestModule" type="Coolite.Ext.Web.AjaxRequestModule, Coolite.Ext.Web" />  
  4.   
  5.  </httpModules>  

 

Coolite.Ext.Web.AjaxRequestModule类增加方法Request

 

view plaincopy to clipboardprint?
  1. public class AjaxRequestModule : IHttpModule  
  2.     {  
  3.         public void Init(HttpApplication app)  
  4.         {  
  5.             app.PostAcquireRequestState += OnPostAcquireRequestState;  
  6.             app.PreSendRequestHeaders += RedirectPreSendRequestHeaders;  
  7.             app.ReleaseRequestState += AjaxRequestFilter;  
  8.             app.BeginRequest +=new EventHandler(app_BeginRequest);  
  9.         }  
  10.         private void app_BeginRequest(Object source, EventArgs e)  
  11.         {  
  12.             HttpApplication application = (HttpApplication)source;  
  13.             HttpContext context = application.Context;  
  14.             //如果请求的路径中含有Coolite,就将其设为UTF-8,这样就不会影响其他GB2312的页面  
  15.             if (!String.IsNullOrEmpty(context.Request["Coolite"]))  
  16.             {  
  17.                 context.Request.ContentEncoding = System.Text.Encoding.UTF8;  
  18.             }  
  19.         }  
  20. 〗  

 

由于时间问题,上面描述比较粗糙。要了解更详细的说明,请看下面:

http://www.velocityreviews.com/forums/t77832-set-request-contentencoding-from-code.html

Set Request.ContentEncoding from code?

Q:I'm having some encoding trouble using my own RequestHandler:

Since the RequestHandler must be able to serve content with diffrent
encoding, I set the Response.Charset and Response.ContentEncoding to the
encoding of current content. Furthermore I set the Content-Type META tag of
the html page to use the same charset as well.

And this all works fine... I get my pages served and both the HTTP headers
and the HTML page has the right encoding applied... Yieppie!!!

But then... when I try to submit a FORM on one of my pages, all my special
characters (like "ãøåöäé") gets lost in the process.

The only way I can preserve the special characters, is by setting the
requestEncoding attribute (specified in <globalization> in web.config) to
the same encoding as I the current Response.ContentEncoding. And that is a
bad solution since all of my requests could be in different encodings.

It doesn't work if i try to set the Response.ContentEncoding to the same as
the Request.ContentEncoding. It's as if the Request already has been decoded
using the wrong format (specified in <globalization> in web.config).

Can anyone please help? I'm I on the wrong track? Is it impossible to serve
diffrent request/response encodings through the same web site? Or am I just
doing it the wrong way?

Thanks for your time,

Ricky

R:Hi Ricky,

From your description, you have met some problem manually setting the
Request's ContentEncoding in ASP.NET via code at runtime, you found it only
works when you specifying them in web.config however what you want it set
different value for each comming request rather than all the same in
web.config , yes?

As for this problem, here are my understanding and suggestions:
The Request/Response 's ContentEncoding set in web.config's Globalization
section is the default setting for the asp.net webapplication. Generally
the comming webrequest is using the encoding from the client(default
setting in browser), if there is no from client, the default setting in
web.config will be used. And also, the Asp.net 's Request and Response
object ContentEncoding can help use manually set them via code. And the
Response's ContentEncoding is nothing particular that we can just set it
before the response content return to client. However, the Request's
ContentEncoding will be a bit different , because when the request comming
, the asp.net runtime will soon checking the ContentEncoding (from client ,
if not, use the default in web.config), So if we set it later in Page's
processing life cycle, it's too late. We must set it before the request is
being processing. In ASP.NET there're several Events during each request's
processing , such as BeginRequest, AuthenticateRequest, EndRequest ...
And the "BeginREquest" is exactly the one we have to use, you can hook this
event in the application 's Global object(Global.asax.cs) or make a custom
HttpModule to handle it. Here are some related reference in MSDN:

#HttpApplication.BeginRequest Event
http://msdn.microsoft.com/library/en...webhttpapplica
tionclassbeginrequesttopic.asp?frame=true

#Custom HttpModule Example
http://msdn.microsoft.com/library/en...tomhttpmodules.
asp?frame=true

Also, following is a former thread discussing on a similiar issue, you may
also have a look:
#query string encoding/decoding
http://groups.google.com/groups?hl=e...readm=PLNLjDdA
EHA.1288%40cpmsftngxa06.phx.gbl&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3
DUTF-8%26oe%3DUTF-8%26q%3Dquerystring%2Bsteven%2Bcheng

Hope helps. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

 

转载地址:http://blog.csdn.net/yls087412/archive/2010/09/29/5914386.aspx


原创粉丝点击