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

    从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\ 目录下

    收功!

  • 相关阅读:
    ajax基本使用
    ajax
    七个你无法忽视的Git使用技巧
    Git原始笔记
    php session自定义处理
    linux下用phpize给PHP动态添加扩展
    【转】做到这一点,你也可以成为优秀的程序员
    PHP扩展开发-测验成功
    PHP扩展开发--实验成功
    php类似shell脚本的用法
  • 原文地址:https://www.cnblogs.com/Microshaoft/p/289665.html
Copyright © 2011-2022 走看看