简单的过滤器

1  新建个类库 添加 system.web的应用

2 实现 IHttpModule的接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace FirstModule

{
class FirstModule :System.Web.IHttpModule
{

//为请求管道的第一个事件 ,也是就BeginRequest  的事件注册一个用户自动以的一个方法

public void Init(HttpApplication app)
{
app.BeginRequest += Fun; 

}
public void Fun(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
app.Context.Response.Write("每个页面前都会出现这句话");

}


public void Dispose()
{
throw new NotImplementedException();
}
}
}

3  在配置文件中有2种方式配置

1 在sysrem web内配置 对应iiss  集成模式

<httpModules>
<add name="first" type="FirstModule.FirstModule"/>
</httpModules>

2 在sysyetm web外配置 对应经典模式

<!--<system.webServer>

<modules>

<add name="first" type="FirstModule.FirstModule"/>

</modules>

</system.webServer>-->

----------------------在全局配置文件中实现过滤器问题

// 利用事件自动机制俄日当前网站的 Application里的事件注册方法
////命名规则 一定要以 Application_ 作为开头
//protected void Application_BeginRequest(object sender, EventArgs e)
//{
// System.Web.HttpApplication app= sender as HttpApplication;
// app.Context.Response.Write("全局事件里配置过滤器");

//}

原文地址:https://www.cnblogs.com/cdaq/p/3579504.html