用 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 zhutou.HttpModules
- {
- using System;
- using System.Web;
- using System.Collections;
- using zhutou.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 StrConvFilter(context.Response.Filter);
- }
- public void Dispose()
- {
- }
- }
- }
- namespace zhutou.IO
- {
- using System;
- using System.IO;
- using System.Web;
- using System.Text;
- using System.Globalization;
- using Microsoft.VisualBasic;
- public class StrConvFilter : Stream
- {
- private Stream _sink;
- private long _position;
- public StrConvFilter(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 /**//* csc.exe /t:library StrConvHttpModule.cs /r:C:\windows\Microsoft.NET\Framework\v1.1.4322\Microsoft.VisualBasic.dll */ namespace zhutou.HttpModules { using System; using System.Web; using System.Collections; using zhutou.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 StrConvFilter(context.Response.Filter); } public void Dispose() { } } } namespace zhutou.IO { using System; using System.IO; using System.Web; using System.Text; using System.Globalization; using Microsoft.VisualBasic; public class StrConvFilter : Stream { private Stream _sink; private long _position; public StrConvFilter(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="zhutou.HttpModules.StrConvHttpModule, StrConvHttpModule" />
</httpModules>
<add name="StrConvHttpModule" type="zhutou.HttpModules.StrConvHttpModule, StrConvHttpModule" />
</httpModules>
将 StrConvHttpModule.dll 复制到 该虚拟目录的 bin\ 目录下
,以及该虚拟目录下的各级子虚拟目录下的 bin\ 目录下