首页 > 技术文章 > asp.net中HttpModule扩展的浅析

williamwsj 2017-08-08 18:20 原文

在asp.net中,我们可以使用HttpModule扩展自己的相关业务。可以在HttpApplication的19个管道事件中注入我们自己的业务逻辑代码。

闲话不说,代码先上。

一、新建网站项目

我们可以在该新建的网站项目里面做自己想要做的开发。

 

二、新建一个HttpModule扩展

我们可以在当前网站项目中自定义自己的HttpModule扩展类,也可以新建一个类库。这两种方式唯一的区别,就是在web.config文件中配置的时候,如果是新建的类库,需要在类全名后面打一个逗号,加上程序集名称。接下来请看:

新建一个类库,命名为MyHttpModule。

在MyHttpModule类库中添加System.Web的引用。因为HttpModule的扩展类需要继承IHttpModule接口。

接下来我们就可以在CustomeModule类中为所欲为啦!!!

IhttpModule有两个方法,即Init和Dispose。

Init方法接收一个HttpApplication实例对象,HttpApplication实例对象代表了当前的应用程序。我们可以在Init方法内部注册HttpApplication暴露出来的任何事件。

请看:

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

namespace MyHttpModule {
    public class CustomModule:IHttpModule {
        //可以在Init方法中注册事件,至于事件的实现需要另外定义
        public void Init(HttpApplication context) {  
            //注册19个事件中的任意事件,也可以注册其他的事件,只要能获取到
            context.BeginRequest += Context_BeginRequest;
            context.EndRequest += Context_EndRequest;
        }       

        private void Context_BeginRequest(object sender, EventArgs e) {
            //只要能获取HttpApplication实例对象,就可以为所欲为
            HttpApplication application = sender as HttpApplication;
            //获取HttpContext对象
            HttpContext httpContext = application.Context;
            //获取HttpRequest
            HttpRequest request = httpContext.Request;
            //获取HttpResponse
            HttpResponse response = httpContext.Response;
            //获取其他需要的...
            //Get more...

            response.Write("<h3 style='color:white;background-color:black;text-align:center;'>开始处理请求...</h3>");
        }

        private void Context_EndRequest(object sender, EventArgs e) {
            //只要能获取HttpApplication实例对象,就可以为所欲为
            HttpApplication application = sender as HttpApplication;
            //获取HttpContext对象
            HttpContext httpContext = application.Context;
            //获取HttpRequest
            HttpRequest request = httpContext.Request;
            //获取HttpResponse
            HttpResponse response = httpContext.Response;
            //获取其他需要的...
            //Get more...

            response.Write("<h3 style='color:white;background-color:black;text-align:center;'>请求处理完毕!</h3>");
        }

        public void Dispose() { }
    }
}

接下来,编译并生成该类库的dll文件。

将MyHttpModule.dll复制到网站项目的bin文件夹中。因为网站项目需要用到该dll文件。

 

接着再配置网站项目的web.config。

<?xml version="1.0" encoding="utf-8"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.0" />
      
      <!-- 在system.web的httpModules节配置我们刚才写的HttpModule扩展类-->
      <httpModules>
        <!--name="自定义模块名称" type="模块类的完全限定名(即需要加上命名空间),外部程序集名称(即dll的名称)-->
        <add name="MyModule" type="MyHttpModule.CustomModule,MyHttpModule"/>
        
        <!--如果是在当前网站项目中编写的HttpModule扩展类,不需要加上程序集名称-->
        <!--<add name="MyModule" type="MyHttpModule.CustomModule"/>-->
      </httpModules>
    </system.web>

</configuration>

接下来让我们见证一下效果,估计会有惊喜:

首先在网站项目中添加一个Default.aspx文件,在该文件中随便输入一些文字。如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="webPro.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>   
    <div>
        这是Default.aspx页面的信息
    </div>   
</body>
</html>

接着运行该页面。

页面运行效果图:

我勒个去,果然有惊吓。于是根据其相关提示,在web.config文件中,将httpModules节配置移动到system.webServer节点里面。

<?xml version="1.0" encoding="utf-8"?>

<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />

    <httpModules>
    </httpModules>
  </system.web>

  <system.webServer>
    <modules>
      <!--name="自定义模块名称" type="模块类的完全限定名(即需要加上命名空间),外部程序集名称(即dll的名称)-->
      <add name="MyModule" type="MyHttpModule.CustomModule,MyHttpModule"/>

      <!--如果是在当前网站项目中编写的HttpModule扩展类,不需要加上程序集名称-->
      <!--<add name="MyModule" type="MyHttpModule.CustomModule"/>-->
    </modules>
  </system.webServer>

</configuration>

再次运行Default.aspx页面,效果如下:

由此看来,我们确实可以在外部程序集中通过继承IHttpModule接口来实现我们自己的HttpModule类,并扩展相应的业务。

 

推荐阅读