要创建自定义模块,类需要实现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>

  程序执行图:

运行时

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. 第九篇 C#实现螺旋矩阵

    C#语言,二维数组的用法和C++.java不同 其它两种也写了,差别不大这里不上传了 using System; namespace _NetRectangle { internal class Pr ...

  2. 2017-2-18 net 输入输出语句

    控制台程序的创建,输出,输入语句,定义变量,变量赋值,值覆盖,值拼接,值打印两种数据类型,整形类型转换 知识点: 1.输出语句 Console.WriteLine("");光标换行 ...

  3. 强化学习读书笔记 - 06~07 - 时序差分学习(Temporal-Difference Learning)

    强化学习读书笔记 - 06~07 - 时序差分学习(Temporal-Difference Learning) 学习笔记: Reinforcement Learning: An Introductio ...

  4. Spring总结_02_Spring概述

    一.概念准备 1.应用程序:是能完成我们所需要功能的成品,比如购物网站.OA系统. 2.框架:是能完成一定功能的半成品,比如我们可以使用框架进行购物网站开发:框架做一部分功能,我们自己做一部分功能,这 ...

  5. swift -- 代理delegate

    1.声明协议 protocol SecondDelagate { func sendValue(text : String!) -> Void } 2.声明代理属性 var delegate : ...

  6. CentOS7搭建Confluence Wiki

    前言 在艾佳生活实习时,有三款团队协作系统特别喜欢:Wiki.Jira和Jenkins.对于Jenkins的搭建,之前<自动部署工具Jenkins>有过记录.这次,搭建一个Wiki,作为知 ...

  7. oracle 11g centos6 安装

    选型:32位的内存是个瓶颈,已经是64位的时代了.使用64位的CentOS6 和 64位的Oracle 11g R2在虚拟机器安装,采用hostonly方式设置网络注意:能上网的网卡要设置一下ICS( ...

  8. C++标准库之queue(各函数及其使用全)

    原创作品,转载请注明出处:http://www.cnblogs.com/shrimp-can/p/5283520.html 一.FIFO队列,即先入先出队列 1.队列的声明 std::deque< ...

  9. [笔记]LR和SVM的相同和不同

    之前一篇博客中介绍了Logistics Regression的理论原理:http://www.cnblogs.com/bentuwuying/p/6616680.html. 在大大小小的面试过程中,经 ...

  10. linux ip 转发设置 ip_forward

    工作原理: 内网主机向公网发送数据包时,由于目的主机跟源主机不在同一网段,所以数据包暂时发往内网默认网关处理,而本网段的主机对此数据包不做任何回应.由于源主机ip是私有的,禁止在公网使用,所以必须将数 ...