让MagicAjax支持中文!

过年啦,回到家一直不能上网,很是一个郁闷,不过大年廿九晚上闲来无事,终于解决了MagicAjax中文乱码的问题。


自从MA 0.2.x发行以来都不能支持JS的中文,比如把JS脚本中的那个"Loading ..."换成"数据加载中 ..."后,再运行时出现的那个条就是乱码。如果更改编码格式页面又会乱掉。

哈哈,下载MA的源代码包,打开MagicAjaxModule.cs文件,看到
None.gif          protected   void  Application_BeginRequest( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            HttpContext context 
= ((HttpApplication)sender).Context;
InBlock.gif
InBlock.gif            
// Init private fields
InBlock.gif
            _threadAbortExceptionThrown = false;
InBlock.gif            _request 
= context.Request;
InBlock.gif            _response 
= context.Response;
InBlock.gif
InBlock.gif
InBlock.gif            
// Create a new context and add it to the items collection for later retrieval
InBlock.gif            
// by MagicAjaxContext.Current
InBlock.gif
            _magicAjaxContext = new MagicAjaxContext();
InBlock.gif            HttpContext.Current.Items.Add(MagicAjaxContext.ContextKey, _magicAjaxContext);
InBlock.gif
InBlock.gif            
// Check if the request is for the embedded AjaxCallObject.js script
InBlock.gif
            if (context.Request.RawUrl.EndsWith("AjaxCallObject.js.aspx"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                context.Response.ContentType 
= "text/javascript";
InBlock.gif                
object cachedAjaxCallObjectJs = context.Cache["__CACHED_AJAXCALLOBJECT_JS"];
InBlock.gif                
if (cachedAjaxCallObjectJs == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//read and output the embedded AjaxCallObject.js file from the manifest
InBlock.gif
                    using (System.IO.StreamReader reader = new System.IO.StreamReader(typeof(MagicAjaxModule).Assembly.GetManifestResourceStream("MagicAjax.script.AjaxCallObject.js")))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        cachedAjaxCallObjectJs 
= reader.ReadToEnd();
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    context.Cache.Insert(
"__CACHED_AJAXCALLOBJECT_JS", cachedAjaxCallObjectJs);
ExpandedSubBlockEnd.gif                }

InBlock.gif                context.Response.Write(cachedAjaxCallObjectJs);
InBlock.gif                context.Response.Cache.SetExpires(DateTime.Now.AddYears(
1));
InBlock.gif                context.Response.End();
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif

噢,这样呀,让它支持GB2312不就行了。

None.gif          protected   void  Application_BeginRequest( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            HttpContext context 
= ((HttpApplication)sender).Context;
InBlock.gif
InBlock.gif            
// Init private fields
InBlock.gif
            _threadAbortExceptionThrown = false;
InBlock.gif            _request 
= context.Request;
InBlock.gif            _response 
= context.Response;
InBlock.gif            _response.ContentEncoding 
= System.Text.Encoding.GetEncoding("GB2312");
InBlock.gif
InBlock.gif            
// Create a new context and add it to the items collection for later retrieval
InBlock.gif            
// by MagicAjaxContext.Current
InBlock.gif
            _magicAjaxContext = new MagicAjaxContext();
InBlock.gif            HttpContext.Current.Items.Add(MagicAjaxContext.ContextKey, _magicAjaxContext);
InBlock.gif
InBlock.gif            
// Check if the request is for the embedded AjaxCallObject.js script
InBlock.gif
            if (context.Request.RawUrl.EndsWith("AjaxCallObject.js.aspx"))
InBlock.gif...

加入了_response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

编译Release文件,然后再使用这个新的DLL就OK了。


不知道,还有没有别的办法,或是以上方法都什么不当之处。

[仅就解决MA支持中文的问题]

转载于:https://www.cnblogs.com/dsclub/archive/2006/02/08/327054.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值