ihttpmodule 接口以及使用

 

我用的是mvc的

1、在model写个类VerificationModule.cs  (名字随便啊)

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
using System.Collections;
using RHRSP.Web.Models;
using System.Xml;
using RHRSP.Web.Controllers.Common;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
public class VerificationModule : IHttpModule
{
    public static int b = 0;
    public VerificationModule()
    {
    }

    public String ModuleName
    {
        get { return "VerificationModule"; }
    }
   
    // In the Init function, register for HttpApplication 
    // events by adding your handlers.
    public void Init(HttpApplication application)
    {                      
       string s= Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
        application.BeginRequest +=
            (new EventHandler(this.Application_BeginRequest));
        application.EndRequest +=
            (new EventHandler(this.Application_EndRequest));
    }

    private void Application_BeginRequest(Object source,
         EventArgs e)
    {
     
        // Create HttpApplication and HttpContext objects to access
        // request and response properties.
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;

      //  string s = context.Request.Url.ToString();
       
        string filePath = context.Request.FilePath;
        string fileExtension =
            VirtualPathUtility.GetExtension(filePath);
        if (fileExtension.Equals("/Index/Index"))
        {         
            context.Response.Write("<h1><font color=red>" +
                "VerificationModule: Beginning of Request" +
                "</font></h1><hr>");
        }
    }

    private void Application_EndRequest(Object source, EventArgs e)
    {        
        HttpApplication application = (HttpApplication)source;
        HttpContext context = application.Context;
        string filePath = context.Request.FilePath;
        string fileExtension =
            VirtualPathUtility.GetExtension(filePath);
        if (fileExtension.Equals(".aspx"))
        {
            context.Response.Write("<hr><h1><font color=red>" +
                "VerificationModule: End of Request</font></h1>");
        }       
    }

    public void Dispose() { }
}

  

2、然后在web.config  添加下面这个

<system.web>
<httpModules>
<add name="VerificationModule" type="VerificationModule" />
</httpModules>

</system.web>

你可能感兴趣的:(Module)