• 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>

  • 相关阅读:
    三元操作符的类型务必一致
    a++ 和 ++a 的区别
    TCP/IP四层协议模型与ISO七层模型
    CentOS 7.0 使用 yum 安装 MariaDB 与 MariaDB 的简单配置
    修改gcc/g++默认include路径
    js中Math.random()生成指定范围数值的随机数
    在 QML 中创建 C++ 导入类型的实例
    QML从文件加载组件简单示例
    Android插件化开发---执行未安装apk中的Service
    游戏开发热门技术浅析
  • 原文地址:https://www.cnblogs.com/wdw31210/p/2505984.html
Copyright © 2020-2023  润新知