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

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

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8.  
  9. namespace ModuleSample
  10. {
  11. public class SampleModule : IHttpModule
  12. {
  13. private const string allowAddressesFile = "AllowedAddresses.txt";
  14. private List<string> allowAddressesList;
  15. public void Dispose()
  16. {
  17. throw new NotImplementedException();
  18. }
  19.  
  20. public void Init(HttpApplication context)
  21. {
  22. context.LogRequest += new EventHandler(OnLogRequest);
  23. context.BeginRequest += BeginRequest;
  24. context.PreRequestHandlerExecute += PreRequestHandlerExecute;
  25. }
  26.  
  27. private void BeginRequest(object sender, EventArgs e)
  28. {
  29. LoadAddresses((sender as HttpApplication).Context);
  30. }
  31. private void LoadAddresses(HttpContext context)
  32. {
  33. if (allowAddressesList == null)
  34. {
  35. string path = context.Server.MapPath(allowAddressesFile);
  36. allowAddressesList = File.ReadAllLines(path).ToList();
  37. }
  38. }
  39. private void PreRequestHandlerExecute(object sender, EventArgs e)
  40. {
  41. HttpApplication app = sender as HttpApplication;
  42. HttpRequest request = app.Context.Request;
  43. if (!allowAddressesList.Contains(request.UserHostAddress))
  44. {
  45. throw new HttpException(403, "IP address denied");
  46. }
  47. }
  48. public void OnLogRequest(Object source, EventArgs e)
  49. {
  50. //custom logging logic can go here
  51. }
  52. }
  53. }

  在WebConfig中配置:

  1. <system.webServer>
  2. <handlers>
  3. <add name="CustomHandler" verb="*" path="CallCustomHandler" type="SampleHandler.CustomHandler,SampleHandler"/>
  4. <add name="InfoHandler" verb="GET" path="CallInfoHandler.axd" type="SampleHandler.InfoHandler,SampleHandler"/>
  5. </handlers>
  6. <modules>
  7. <add name="SampleModule" type="ModuleSample.SampleModule,ModuleSample"/>
  8. </modules>
  9. </system.webServer>

  程序执行图:

运行时

ASP.NET自定义模块的更多相关文章

  1. asp.net通用查询模块设计

    asp.net通用查询模块设计 前言 自从上次狂喷了devexpress for asp.net面向互联网的app的各种不合理,好像骂的dev无处容身了,不过说实话,dev在做互联网的app时,生成的 ...

  2. [转]ASP.NET 核心模块配置参考

    本文转自:https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/aspnet-core-module?view=aspnetcore ...

  3. 连表查询都用Left Join吧 以Windows服务方式运行.NET Core程序 HTTP和HTTPS的区别 ASP.NET SignalR介绍 asp.net—WebApi跨域 asp.net—自定义轻量级ORM C#之23中设计模式

    连表查询都用Left Join吧   最近看同事的代码,SQL连表查询的时候很多时候用的是Inner Join,而我觉得对我们的业务而言,99.9%都应该使用Left Join(还有0.1%我不知道在 ...

  4. ASP.NET Core模块概述

    原文地址:ASP.NET Core Module overview By Tom Dykstra, Rick Strahl, and Chris Ross ASP.NET Core模块(ANCM)让你 ...

  5. 【python】用setup安装自定义模块和包

    python解释器查找module进行加载的时候,查找的目录是存放在sys.path变量中的,sys.path变量中包含文件的当前目录.如果你想使用一个存放在其他目录的脚本,或者是其他系统的脚本,你可 ...

  6. angular(3)服务 --注入---自定义模块--单页面应用

    ng内部,一旦发生值改变操作,如$scope.m=x,就会自动轮询$digest队列,触发指定的$watch,调用其回调函数,然后修改dom树. 干货:https://github.com/xufei ...

  7. python基础知识8——模块1——自定义模块和第三方开源模块

    模块的认识 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需 ...

  8. (02)odoo自定义模块

    * 官方建议模块骨架    --------------------------    addons/<my_module_name>/                 │─ __init ...

  9. Func系列3:自定义模块

    简介 Func自带的模块已经非常丰富,但在日常系统运维当中,尤其是面对大规模的服务器集群.不同类别的业务平台,次是Func自带的模块或许已经不能满足我们的需求,所以有必要通过自定义模块来填补这块的不足 ...

随机推荐

  1. Java编程风格学习(三)

    在上一篇的java编程风格学习(二)中我们学习了一些在Java编码过程中的格式规范,遵循这些规范毋庸置疑是我们的书写高质量代码的前提与基础.今天我们更进一步,一起来学习Java编程的命名规范,向着编写 ...

  2. Protege5.0.0入门学习

    OWL本体的重要组成部分 Individuals:个体,代表一个领域里面的对象.可以理解成一个类的实例(instances of classes). Properties:属性,是两个个体之间的双重联 ...

  3. 【转】Netty系列之Netty并发编程分析

    http://www.infoq.com/cn/articles/netty-concurrent-programming-analysis

  4. Dapper C# 访问SQLite

    1.以操作SQLite为例.先下载Dapper,项目引用添加Dapper.dll,然后入下 SQLiteConnectionStringBuilder sb = new SQLiteConnectio ...

  5. 服务器 'XXXXXX' 上的 MSDTC 不可用。解决方法

    今天在C#中操作数据库的时候突然发现这个问题:服务器 'USER-XXX' 上的 MSDTC 不可用. 解决方法: 在windows控制面板 --> 管理工具 --> 服务 --> ...

  6. 禁止Linux系统被 ping

    echo "net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf sysctl -p 生效 开启ping功能: 删除 ...

  7. leetcode HouseRobber Dp Code

    #include <malloc.h> int MAX(int x,int y){ return x>y?x:y;} int rob(int* nums, int numsSize) ...

  8. MATLAB(5)——生成归一化直方图

    作者:桂. 时间:2017-03-10  22:13:36 链接:http://www.cnblogs.com/xingshansi/p/6533579.html 声明:欢迎转载,不过记得注明出处哦~ ...

  9. JSOI2015 一轮省选 个人题解与小结

    T1: 题目大意:现有一个以1为根节点的树,要求从1开始出发,经过下面的点然后最终要回到根节点.同时除了根节点之外各点均有一个权值(即受益,每个点上的收益只能拿一次,且经过的话必须拿),同时除了根节点 ...

  10. 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 432  Solved: 270[ ...