HttpModule 实现 ASP.Net (*.aspx) 中文简繁体的自动转换,不用修改原有的任何代码,直接部署即可!...

用 HttpModule 实现了 ASP.Net (*.aspx) 中文简繁体的自动转换!
思路相当简单!
Global.asax 的 Codebehind 的 Application_BeginRequest 的事件处理函数也应可以实现!
HttpHandler 是不能实现的,因为它是"截流"!


效果不错!可以处理任意 ASP.Net 站点、虚拟目录!不用修改原有的任何代码!
代码如下:

C#代码 复制代码
  1. StrConvHttpModule.cs   
  2. /**//*  
  3. csc.exe /t:library StrConvHttpModule.cs /r:C:\windows\Microsoft.NET\Framework\v1.1.4322\Microsoft.VisualBasic.dll   
  4. */  
  5. namespace zhutou.HttpModules   
  6. {   
  7.     using System;   
  8.     using System.Web;    
  9.     using System.Collections;   
  10.   
  11.     using zhutou.IO;   
  12.   
  13.     public class StrConvHttpModule : IHttpModule   
  14.     {   
  15.         public string ModuleName   
  16.         {   
  17.             get  
  18.             {   
  19.                 return "StrConvHttpModule";   
  20.             }   
  21.         }   
  22.   
  23.         public void Init(HttpApplication application)   
  24.         {   
  25.             application.BeginRequest += (new EventHandler(this.Application_BeginRequest));   
  26.         }   
  27.            
  28.         private void Application_BeginRequest(object sender, EventArgs e)   
  29.         {   
  30.             HttpApplication application = (HttpApplication) sender;   
  31.             HttpContext context = application.Context;   
  32.             context.Response.Filter = new StrConvFilter(context.Response.Filter);   
  33.         }   
  34.   
  35.         public void Dispose()   
  36.         {   
  37.         }   
  38.     }   
  39. }   
  40.   
  41. namespace zhutou.IO   
  42. {   
  43.     using System;   
  44.     using System.IO;   
  45.     using System.Web;   
  46.     using System.Text;   
  47.     using System.Globalization;   
  48.   
  49.     using Microsoft.VisualBasic;   
  50.   
  51.     public class StrConvFilter : Stream   
  52.     {   
  53.         private Stream _sink;   
  54.         private long _position;   
  55.   
  56.         public StrConvFilter(Stream sink)   
  57.         {   
  58.             this._sink = sink;   
  59.         }   
  60.   
  61.         public override bool CanRead   
  62.         {   
  63.             get  
  64.             {   
  65.                 return true;   
  66.             }   
  67.         }   
  68.   
  69.         public override bool CanSeek   
  70.         {   
  71.             get  
  72.             {   
  73.                 return true;   
  74.             }   
  75.         }   
  76.   
  77.         public override bool CanWrite   
  78.         {   
  79.             get  
  80.             {   
  81.                 return true;   
  82.             }   
  83.         }   
  84.   
  85.         public override long Length   
  86.         {   
  87.             get  
  88.             {   
  89.                 return 0;   
  90.             }   
  91.         }   
  92.   
  93.         public override long Position   
  94.         {   
  95.             get  
  96.             {   
  97.                 return this._position;   
  98.             }   
  99.         set  
  100.             {   
  101.                 this._position = value;   
  102.             }   
  103.         }   
  104.   
  105.         public override long Seek(long offset, SeekOrigin direction)   
  106.         {   
  107.             return this._sink.Seek(offset, direction);   
  108.         }   
  109.   
  110.         public override void SetLength(long length)   
  111.         {   
  112.             this._sink.SetLength(length);   
  113.         }   
  114.   
  115.         public override void Close()   
  116.         {   
  117.             this._sink.Close();   
  118.         }   
  119.   
  120.         public override void Flush()   
  121.         {   
  122.             this._sink.Flush();   
  123.         }   
  124.   
  125.         public override int Read(byte[] buffer, int offset, int count)   
  126.         {   
  127.             return this._sink.Read(buffer, offset, count);   
  128.         }   
  129.   
  130.         public override void Write(byte[] buffer, int offset, int count)   
  131.         {   
  132.             if (HttpContext.Current.Response.ContentType == "text/html")   
  133.             {   
  134.                 Encoding e = Encoding.GetEncoding(HttpContext.Current.Response.Charset);   
  135.                 string s = e.GetString(buffer, offset, count);   
  136.                 s = Strings.StrConv(s, VbStrConv.TraditionalChinese, CultureInfo.CurrentCulture.LCID);   
  137.                 this._sink.Write(e.GetBytes(s), 0, e.GetByteCount(s));   
  138.             }   
  139.             else  
  140.             {   
  141.                 this._sink.Write(buffer, offset, count);   
  142.             }   
  143.         }   
  144.     }   
  145. }  
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>


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

转载于:https://www.cnblogs.com/xiachufeng/archive/2010/07/22/1783335.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值