zoukankan      html  css  js  c++  java
  • ASP.NET自定义模块

    要创建自定义模块,类需要实现IHttpModule接口。这个接口定义了Init和Dispose方法。

    Init方法在启动Web应用程序时调用,其参数的类型是HttpContext,可以添加应用程序处理事件。

    新建类库ModuleSample,新建类SampleModule添加如下代码:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Web;
    
    namespace ModuleSample
    {
        public class SampleModule : IHttpModule
        {
            private const string allowAddressesFile = "AllowedAddresses.txt";
            private List<string> allowAddressesList;
            public void Dispose()
            {
                throw new NotImplementedException();
            }
    
            public void Init(HttpApplication context)
            {
                context.LogRequest += new EventHandler(OnLogRequest);
                context.BeginRequest += BeginRequest;
                context.PreRequestHandlerExecute += PreRequestHandlerExecute;
            }
    
            private void BeginRequest(object sender, EventArgs e)
            {
                LoadAddresses((sender as HttpApplication).Context);
            }
            private void LoadAddresses(HttpContext context)
            {
                if (allowAddressesList == null)
                {
                    string path = context.Server.MapPath(allowAddressesFile);
                    allowAddressesList = File.ReadAllLines(path).ToList();
                }
            }
            private void PreRequestHandlerExecute(object sender, EventArgs e)
            {
                HttpApplication app = sender as HttpApplication;
                HttpRequest request = app.Context.Request;
                if (!allowAddressesList.Contains(request.UserHostAddress))
                {
                    throw new HttpException(403, "IP address denied");
                }
            }
            public void OnLogRequest(Object source, EventArgs e)
            {
                //custom logging logic can go here
            }
        }
    }
    

      在WebConfig中配置:

     <system.webServer>
        <handlers>
          <add name="CustomHandler" verb="*" path="CallCustomHandler" type="SampleHandler.CustomHandler,SampleHandler"/>
          <add name="InfoHandler" verb="GET" path="CallInfoHandler.axd" type="SampleHandler.InfoHandler,SampleHandler"/>
        </handlers>
        <modules>
          <add name="SampleModule" type="ModuleSample.SampleModule,ModuleSample"/>
        </modules>
      </system.webServer>
    

      程序执行图:

    运行时

  • 相关阅读:
    BZOJ 1818: [Cqoi2010]内部白点 扫描线+树状数组
    BZOJ 2091: [Poi2010]The Minima Game 博弈dp
    BZOJ 4459: [Jsoi2013]丢番图 数学推导
    BZOJ 3561: DZY Loves Math VI 莫比乌斯反演+复杂度分析
    BZOJ 3048: [Usaco2013 Jan]Cow Lineup 双指针
    PAT Basic 1012 数字分类 (20 分)
    PAT Basic 1008 数组元素循环右移问题 (20 分)
    大数据数据库HBase(一)——架构原理
    PAT Basic 1046 划拳 (15 分)
    PAT Basic 1026 程序运行时间 (15 分)
  • 原文地址:https://www.cnblogs.com/simen-tan/p/5427562.html
Copyright © 2011-2022 走看看