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>
程序执行图:
运行时
ASP.NET自定义模块的更多相关文章
- asp.net通用查询模块设计
asp.net通用查询模块设计 前言 自从上次狂喷了devexpress for asp.net面向互联网的app的各种不合理,好像骂的dev无处容身了,不过说实话,dev在做互联网的app时,生成的 ...
- [转]ASP.NET 核心模块配置参考
本文转自:https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/aspnet-core-module?view=aspnetcore ...
- 连表查询都用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%我不知道在 ...
- ASP.NET Core模块概述
原文地址:ASP.NET Core Module overview By Tom Dykstra, Rick Strahl, and Chris Ross ASP.NET Core模块(ANCM)让你 ...
- 【python】用setup安装自定义模块和包
python解释器查找module进行加载的时候,查找的目录是存放在sys.path变量中的,sys.path变量中包含文件的当前目录.如果你想使用一个存放在其他目录的脚本,或者是其他系统的脚本,你可 ...
- angular(3)服务 --注入---自定义模块--单页面应用
ng内部,一旦发生值改变操作,如$scope.m=x,就会自动轮询$digest队列,触发指定的$watch,调用其回调函数,然后修改dom树. 干货:https://github.com/xufei ...
- python基础知识8——模块1——自定义模块和第三方开源模块
模块的认识 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需 ...
- (02)odoo自定义模块
* 官方建议模块骨架 -------------------------- addons/<my_module_name>/ │─ __init ...
- Func系列3:自定义模块
简介 Func自带的模块已经非常丰富,但在日常系统运维当中,尤其是面对大规模的服务器集群.不同类别的业务平台,次是Func自带的模块或许已经不能满足我们的需求,所以有必要通过自定义模块来填补这块的不足 ...
随机推荐
- Java编程风格学习(三)
在上一篇的java编程风格学习(二)中我们学习了一些在Java编码过程中的格式规范,遵循这些规范毋庸置疑是我们的书写高质量代码的前提与基础.今天我们更进一步,一起来学习Java编程的命名规范,向着编写 ...
- Protege5.0.0入门学习
OWL本体的重要组成部分 Individuals:个体,代表一个领域里面的对象.可以理解成一个类的实例(instances of classes). Properties:属性,是两个个体之间的双重联 ...
- 【转】Netty系列之Netty并发编程分析
http://www.infoq.com/cn/articles/netty-concurrent-programming-analysis
- Dapper C# 访问SQLite
1.以操作SQLite为例.先下载Dapper,项目引用添加Dapper.dll,然后入下 SQLiteConnectionStringBuilder sb = new SQLiteConnectio ...
- 服务器 'XXXXXX' 上的 MSDTC 不可用。解决方法
今天在C#中操作数据库的时候突然发现这个问题:服务器 'USER-XXX' 上的 MSDTC 不可用. 解决方法: 在windows控制面板 --> 管理工具 --> 服务 --> ...
- 禁止Linux系统被 ping
echo "net.ipv4.icmp_echo_ignore_all=1" >> /etc/sysctl.conf sysctl -p 生效 开启ping功能: 删除 ...
- leetcode HouseRobber Dp Code
#include <malloc.h> int MAX(int x,int y){ return x>y?x:y;} int rob(int* nums, int numsSize) ...
- MATLAB(5)——生成归一化直方图
作者:桂. 时间:2017-03-10 22:13:36 链接:http://www.cnblogs.com/xingshansi/p/6533579.html 声明:欢迎转载,不过记得注明出处哦~ ...
- JSOI2015 一轮省选 个人题解与小结
T1: 题目大意:现有一个以1为根节点的树,要求从1开始出发,经过下面的点然后最终要回到根节点.同时除了根节点之外各点均有一个权值(即受益,每个点上的收益只能拿一次,且经过的话必须拿),同时除了根节点 ...
- 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 432 Solved: 270[ ...