动态修改asp.net的title值

using System;
using System.IO ;
using System.Text ;
using System.Text.RegularExpressions ;
namespace dhzCry
{
 public class FilterModule:System.Web.IHttpModule
 {
  public FilterModule()
  {}

  protected void OnPreSendRequestContent(Object sender, EventArgs e)
  {
   System.Web.HttpApplication myContext = (System.Web.HttpApplication)sender;
   //毒藥就是從Response.Filter這裡放進來,至於毒藥下一步再作
   myContext.Response.Filter = new UpperCaseFilter(myContext.Response.Filter);
  }
  public void Init(System.Web.HttpApplication context)
  {
    context.PostRequestHandlerExecute+= new EventHandler(this.OnPreSendRequestContent);
  //   context.PreSendRequestContent+= new EventHandler(this.OnPreSendRequestContent);
  // context.BeginRequest+=new EventHandler(this.OnPreSendRequestContent);
  
  }
  public void Dispose()
  {
  }
 }
 /// <summary>
 /// UpperCaseFilter 毒藥
 /// </summary>
 public class UpperCaseFilter : Stream
  // This filter changes all characters passed through it to uppercase.
 {
  private Stream _sink;
  private long _position;
     private System.Text.Encoding end=System.Text.Encoding.GetEncoding("GB18030");
  private System.Text.StringBuilder  oOutput = new StringBuilder();

  public UpperCaseFilter(Stream sink)
  {
   _sink = sink;
   
  }

  // The following members of Stream must be overriden.
  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 _position; }
   set { _position = value; }
  }

  public override long Seek(long offset, System.IO.SeekOrigin direction)
  {
   return _sink.Seek(offset, direction);
  }

  public override void SetLength(long length)
  {
   _sink.SetLength(length);
  }

  public override void Close()
  {
   _sink.Close();
  }

  public override void Flush()
  {
   _sink.Flush();
  }

  public override int Read(byte[] buffer, int offset, int count)
  {
   return _sink.Read(buffer, offset, count);
  }

  // The Write method actually does the filtering.
  public override void Write(byte[] buffer, int offset, int count)
  {
      string szBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);

    //Look for the end of the HTML file
    //查找HTML文件的结尾部分
    Regex oEndFile = new Regex("</head>", RegexOptions.IgnoreCase);
    if (oEndFile.IsMatch(szBuffer))
    {
     //Append the last buffer of data
     //附加上缓冲区中的最后一部分数据
     oOutput.Append(szBuffer);
       
     //Get back the complete response for the client
     //传回完整的客户端返回数据
     string szCompleteBuffer = oOutput.ToString();
       
     Regex oRegEx = new Regex(@"<Title>/w*</title>", RegexOptions.IgnoreCase);
     if (oRegEx.IsMatch(szCompleteBuffer))
     {
     //Found string, so replace all occurences
     //with the new value (use a non-case sensitive
     // match)
     //已经找到字符串,所以讲所有的匹配项目用新的值来替换(不区分大小写)
     string newBuffer = Regex.Replace(szCompleteBuffer,@"<Title>.*</title>", "<TITLE>dhz</TITLE>", RegexOptions.IgnoreCase);
         
     //Set the reference so can use same code below...
     //设置引用,因此可以重复使用相同的代码...
     szCompleteBuffer = newBuffer;
     }

     //No match, so write out original data
     //没有匹配,因此写入源代码
     byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(szCompleteBuffer);
     _sink.Write(data, 0, data.Length);
    }
    else
    {
     oOutput.Append(szBuffer);
    }

   
 

  
      
  }
 }
}


/

 <httpModules>
         <add type="dhzCry.FilterModule,dhzCry" name="FilterModule"/>
       </httpModules>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值