C#使用IHttpModule接口修改http输出的方法浅谈

时间:2023-03-09 06:43:09
C#使用IHttpModule接口修改http输出的方法浅谈

一、但你每次请求浏览一个页面,比如Login.aspx的时候,都会执行配置文件中system.webserver内的model这个节点的东西(这个是属于遍历的逻辑执行,会将model这个节点的东西全部执行完才执行其他东西)

二、当model这个节点内存在自己定义要先执行的类,如

   <modules>
<add name="Web.Core" type="Web.Core.UrlRewrite,Web.Core" />
</modules>

这会执行这个类。

但是若在这个类中有context.Response.End();这个者浏览器请求的时候不会返回页面,因为在这里终止了,代码如下:浏览器只会返回aaa,而不会返回当前请求的页面

//修改http输出先建个类这个类作为模块mould就要实现接口
namespace Web.Core
{
   //实现接口
public class UrlRewrite : IHttpModule
{
    //点击实现接口就会出来以下对应的属性和一个方法
public void Dispose() //处理属性
{ } public void Init(HttpApplication context)
{
       //当应用开始请求时,beginRequest是一个事件用委托定义事件
context.BeginRequest += new EventHandler(context_BeginRequest);
}
     //定义个下面要用的当前请求对象变量初值为null
HttpContext context;
void context_BeginRequest(object sender, EventArgs e) //事件的处理方法
{
HttpApplication app = (HttpApplication)sender;
       //给当前请求context赋值,Context获取当前请求的Http特定信息
context = app.Context;
context.Response.Write("aaa");
context.Response.End();
} }
}

三、若没有在modules节点的类中写context.Response.End();,则会向下执行handlers这个节点,然后返回你所请求的方法