从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
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//*
csc.exe /t:library StrConvHttpModule.cs /r:C:\windows\Microsoft.NET\Framework\v1.1.4322\Microsoft.VisualBasic.dll
*/
namespace Microshaoft.HttpModules
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
using System;
using System.Web;
using System.Collections;
![](/Images/OutliningIndicators/InBlock.gif)
using Microshaoft.IO;
![](/Images/OutliningIndicators/InBlock.gif)
public class StrConvHttpModule : IHttpModule
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public string ModuleName
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "StrConvHttpModule";
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public void Init(HttpApplication application)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpApplication application = (HttpApplication) sender;
HttpContext context = application.Context;
context.Response.Filter = new StrConvFilterStream(context.Response.Filter);
}
![](/Images/OutliningIndicators/InBlock.gif)
public void Dispose()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
}
}
![](/Images/OutliningIndicators/None.gif)
namespace Microshaoft.IO
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
using System;
using System.IO;
using System.Web;
using System.Text;
using System.Globalization;
![](/Images/OutliningIndicators/InBlock.gif)
using Microsoft.VisualBasic;
![](/Images/OutliningIndicators/InBlock.gif)
public class StrConvFilterStream : Stream
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private Stream _sink;
private long _position;
![](/Images/OutliningIndicators/InBlock.gif)
public StrConvFilterStream(Stream sink)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._sink = sink;
}
![](/Images/OutliningIndicators/InBlock.gif)
public override bool CanRead
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public override bool CanSeek
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public override bool CanWrite
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public override long Length
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return 0;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public override long Position
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._position;
}
set
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._position = value;
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public override long Seek(long offset, SeekOrigin direction)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._sink.Seek(offset, direction);
}
![](/Images/OutliningIndicators/InBlock.gif)
public override void SetLength(long length)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._sink.SetLength(length);
}
![](/Images/OutliningIndicators/InBlock.gif)
public override void Close()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._sink.Close();
}
![](/Images/OutliningIndicators/InBlock.gif)
public override void Flush()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this._sink.Flush();
}
![](/Images/OutliningIndicators/InBlock.gif)
public override int Read(byte[] buffer, int offset, int count)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return this._sink.Read(buffer, offset, count);
}
![](/Images/OutliningIndicators/InBlock.gif)
public override void Write(byte[] buffer, int offset, int count)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (HttpContext.Current.Response.ContentType == "text/html")
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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\ 目录下
收功!