從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
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\ 目錄下
收功!
轉載于:https://www.cnblogs.com/Microshaoft/archive/2005/12/03/289665.html