msn㊣Microshaoft.com 

naIBnaiM nOracle (大舌头) I Love download EditPlus.v3.11(340)!

© 0000 - 9999 ㊣ Microshaoft ™ ®
砍头不要紧 爱情价更高 欲穷千里目 粒粒皆辛苦 身披七彩祥云 脚踏金甲圣衣 卧鼠藏虫
http://microshaoft.googlepages.com/

博客园 首页 新随笔 联系 订阅 管理
  78 Posts :: 0 Stories :: 366 Comments :: 6 Trackbacks
从8月份忙到现在还没忙完,还得再坚持几个月!
一直没时间更新blog,今天收到一封回复我以前的一篇blog的邮件,提醒了我!
http://Microshaoft.cnblogs.com/archive/2005/03/22/123365.aspx#288944
于是根据该网友的建议:
用 HttpModule 实现了 ASP.Net (*.aspx) 中文简繁体的自动转换!
思路相当简单!
Global.asax 的 Codebehind 的 Application_BeginRequest 的事件处理函数也应可以实现!
HttpHandler 是不能实现的,因为它是"截流"!


效果不错!可以处理任意 ASP.Net 站点、虚拟目录!不用修改原有的任何代码!
代码如下:
StrConvHttpModule.cs
/*
csc.exe /t:library StrConvHttpModule.cs /r:C:\windows\Microsoft.NET\Framework\v1.1.4322\Microsoft.VisualBasic.dll 
*/

namespace Microshaoft.HttpModules
{
    
using System;
    
using System.Web; 
    
using System.Collections;

    
using Microshaoft.IO;

    
public class StrConvHttpModule : IHttpModule
    
{
        
public string ModuleName
        
{
            
get
            
{
                
return "StrConvHttpModule";
            }

        }


        
public void Init(HttpApplication application)
        
{
            application.BeginRequest 
+= (new EventHandler(this.Application_BeginRequest));
        }

        
        
private void Application_BeginRequest(object sender, EventArgs e)
        
{
            HttpApplication application 
= (HttpApplication) sender;
            HttpContext context 
= application.Context;
            context.Response.Filter 
= new StrConvFilterStream(context.Response.Filter);
        }


        
public void Dispose()
        
{
        }

    }

}


namespace Microshaoft.IO
{
    
using System;
    
using System.IO;
    
using System.Web;
    
using System.Text;
    
using System.Globalization;

    
using Microsoft.VisualBasic;

    
public class StrConvFilterStream : Stream
    
{
        
private Stream _sink;
        
private long _position;

        
public StrConvFilterStream(Stream sink)
        
{
            
this._sink = sink;
        }


        
public override bool CanRead
        
{
            
get
            
{
                
return true;
            }

        }


        
public override bool CanSeek
        
{
            
get
            
{
                
return true;
            }

        }


        
public override bool CanWrite
        
{
            
get
            
{
                
return true;
            }

        }


        
public override long Length
        
{
            
get
            
{
                
return 0;
            }

        }


        
public override long Position
        
{
            
get
            
{
                
return this._position;
            }

        
set
            
{
                
this._position = value;
            }

        }


        
public override long Seek(long offset, SeekOrigin direction)
        
{
            
return this._sink.Seek(offset, direction);
        }


        
public override void SetLength(long length)
        
{
            
this._sink.SetLength(length);
        }


        
public override void Close()
        
{
            
this._sink.Close();
        }


        
public override void Flush()
        
{
            
this._sink.Flush();
        }


        
public override int Read(byte[] buffer, int offset, int count)
        
{
            
return this._sink.Read(buffer, offset, count);
        }


        
public override void Write(byte[] buffer, int offset, int count)
        
{
            
if (HttpContext.Current.Response.ContentType == "text/html")
            
{
                Encoding e 
= Encoding.GetEncoding(HttpContext.Current.Response.Charset);
                
string s = e.GetString(buffer, offset, count);
                s 
= Strings.StrConv(s, VbStrConv.TraditionalChinese, CultureInfo.CurrentCulture.LCID);
                
this._sink.Write(e.GetBytes(s), 0, e.GetByteCount(s));
            }

            
else
            
{
                
this._sink.Write(buffer, offset, count);
            }

        }

    }

}

将 StrConvHttpModule.cs 编译为 StrConvHttpModule.dll:
csc.exe /t:library StrConvHttpModule.cs /r:C:\windows\Microsoft.NET\Framework\v1.1.4322\Microsoft.VisualBasic.dll

以 Microsoft .NET Framework SDK 自带的  QuickStart 教程站点为例
http://localhost/quickstart/
修改 quickstart 虚拟目录下的 web.config, 在 <system.web>...</system.web> 区域添加如下配置节:

    <httpModules>
        
<add name="StrConvHttpModule" type="Microshaoft.HttpModules.StrConvHttpModule, StrConvHttpModule" />
    
</httpModules>


将 StrConvHttpModule.dll 复制到 该虚拟目录的 bin\ 目录下
,以及该虚拟目录下的各级子虚拟目录下的 bin\ 目录下

收功!

posted on 2005-12-03 00:27 Microshaoft 阅读(2590) 评论(11)  编辑 收藏

Feedback

#1楼 2005-12-03 01:34 颓废边缘      
这个方法上次研究了下 不过我觉得每次将html流进行即时翻译~对系统的性能会造成额外的负荷~所以最终不是很理想
  回复  引用  查看    

#2楼 2005-12-03 11:15 fuyude.net      
如果只是很少人访问的时候用到 繁体 这个方法还是值得考虑的。最好的地方是它是即时翻译的。
  回复  引用  查看    

#3楼 2006-02-02 12:43 王天[未注册用户]
但根據以前似乎是坦裝某期的說法也不是問題多大,能解決的
  回复  引用    

#4楼 2006-04-15 11:46 c8530[未注册用户]
c#中怎么用? 谢谢!
  回复  引用    

#5楼 2006-07-09 21:48 horse[未注册用户]
我个人觉得本程序只是利用了简体汉字编码中的繁体字形,并没有真正的转换成BIG5内码,所以在香港、台湾等地将显示为乱码。但本程序的思路很好,对我有启发作用。谢谢!
yzqhorse@hotmail.com

  回复  引用    

#6楼 2006-12-18 11:49 coolma[未注册用户]
utf-8编码,和big5无关,香港、台湾等地正确显示的
  回复  引用    

#7楼 2007-01-18 14:41 Bryan[未注册用户]
的確很簡便,但這種方法不可以實現用戶定制.
  回复  引用    

#8楼 2007-01-18 14:41 Bryan[未注册用户]
的確很簡便,但這種方法不可以實現用戶定制.
  回复  引用    

#9楼 2007-08-28 19:48 antonius[未注册用户]
要加的是
Response.Charset = "big5";
不然就是乱的

  回复  引用    

#10楼 2007-09-21 08:10 1-2-3      
原来还可以这样,受教了。
  回复  引用  查看    

#11楼 2008-09-03 08:42 itbird[未注册用户]
非常好的办法,但是有一个问题是,如果网站需要同时支持简体繁体,譬如说有一个选项可以指定访问那种,那我们如果修改呢?
现在,我可以很轻易的把整个简体网站转换为繁体,但是否意味着如果要支持繁体的话,我需要维护两套代码呢?
谢谢。

  回复  引用    




发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 289665


相关文章:

相关链接: