ASP.NET处理HTTP请求的流程:IHttpModule、IHttpHandler与管道事件
作者:springsnow 发布时间:2024-05-09 09:04:58
一、ASP.NET处理管道
Asp.net处理管道的第一步是创建HttpWorkerRequest对象,它包含于当前请求有关的所有信息。
HttpWorkerRequest把请求传递给HttpRuntime类的静态ProcessRequest方法。HttpRuntime首先要做的事是创建HttpContext对象,并用HttpWorkerRequest进行初始化。
创建了HttpContext实例之后,HttpRuntime类就通过调用HttpApplicationFactory的静态GetApplicationInstance()方法,为该应用程序请求HttpApplication派生类的一个示例。GetApplicationInstance()方法要么创建一个HttpApplication类的一个新实例,要么从应用程序对象池中取出一个实例。
在创建完成HttpApplication实例之后,就对它进行初始化,并在初始化期间分配应用程序定义的所
有模块。模块式实现IHttpModule接口的类,作用就是为了实现那经典的19个标准处理事件。
在创建了模块之后,HttpRuntime类通过调用它的BeginProcessRequest方法,要求最新检索到的HttpApplication类对当前请求提供服务。然后,为当前请求找到合适的处理程序工厂。
创建处理程序,传递当前HttpContext,一旦ProcessRequest方法返回,请求完成。
二、IHttpHandler
HttpHandler是asp.net真正处理Http请求的地方。在这个HttpHandler容器中,ASP.NET Framework才真正地对客户端请求的服务器页面做出编译和执行,并将处理过后的信息附加在HTTP请求信息流中再次返回到HttpModule中。
当一个HTTP请求经过HttpModule容器传递到HttpHandler容器中时,ASP.NET Framework会调用HttpHandler的ProcessRequest成员方法来对这个HTTP请求进行真正的处理。并将处理完成的结果继续经由HttpModule传递下去,直至到达客户端。
HttpHandler与HttpModule不同,一旦定义了自己的HttpHandler类,那么它对系统的HttpHandler的关系将是“覆盖”关系。
应用1:图片防盗链(实现一个自定义的IHttpHandler)
第一:定义一个实现了IHttpHandler的类,并且实现其ProcessRequest方法。在一个HttpHandler容器中如果需要访问Session,必须实现IRequiresSessionState接口,这只是一个标记接口,没有任何方法。
public class PictureHttpHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
//站点的域名
string myDomain = "localhost";
if (context.Request.UrlReferrer == null ||
context.Request.UrlReferrer.Host.ToLower().IndexOf(myDomain) < 0)
{
//如果是通过浏览器直接访问或者是通过其他站点访问过来的,则显示“资源不存在”图片
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile(context.Request.PhysicalApplicationPath + "/images/noimg.jpg");
}
else
{
//如果是通过站内访问的,这正常显示图片
context.Response.ContentType = "image/JPEG";
context.Response.WriteFile(context.Request.PhysicalPath);
}
}
}
第二:在web.config中注册这个类,并且指定Handler处理的请求类型,把此节点插入system.web节点中
<httpHandlers>
<!--path中指定的是执行type中HttpHandler的访问路径。此路径可以带后缀也可以不带后缀。如果path配置为*,则会对所有的请求执行此HttpHandler-->
<add verb="*" path="*.jpg" type="MyHttpHandler.PictureHttpHandler,MyHttpHandler"/>
</httpHandlers>
正常访问default页面时:
通过图片地址直接访问时:
应用2、生成验证码
public class ValidateCodeHttpHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/gif";
//建立Bitmap对象,绘图
Bitmap basemap = new Bitmap(200, 60);
Graphics graph = Graphics.FromImage(basemap);
graph.FillRectangle(new SolidBrush(Color.White), 0, 0, 200, 60);
Font font = new Font(FontFamily.GenericSerif, 48, FontStyle.Bold, GraphicsUnit.Pixel);
Random r = new Random();
string letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
string letter;
StringBuilder s = new StringBuilder();
//添加随机的五个字母
for (int x = 0; x < 5; x++)
{
letter = letters.Substring(r.Next(0, letters.Length - 1), 1);
s.Append(letter);
graph.DrawString(letter, font, new SolidBrush(Color.Black), x * 38, r.Next(0, 15));
}
//混淆背景
Pen linePen = new Pen(new SolidBrush(Color.Black), 2);
for (int x = 0; x < 6; x++)
graph.DrawLine(linePen, new Point(r.Next(0, 199), r.Next(0, 59)),
new Point(r.Next(0, 199), r.Next(0, 59)));
//将图片保存到输出流中
basemap.Save(context.Response.OutputStream, ImageFormat.Gif);
//context.Session["CheckCode"] = s.ToString();
//如果没有实现IRequiresSessionState,则这里会出错,也无法生成图片
context.Response.End();
}
public bool IsReusable
{
get { return true; }
}
}
把下面的项加到web.config中的httphandler节点中:
<add verb="*" path="validatevode" type="MyHttpHandler.ValidateCodeHttpHandler,MyHttpHandler"/>
访问validatevode时:
三、自定义HttpModule:
每次请求的开始和结束定义的HttpModule。
在Asp.net中,创建在System.Web命名空间下的IHttpModule接口专门用来定义HttpApplication对象的事件处理。实现IHttpModule接口的类称为HttpModule(Http模块)。
1、通过IHttpModule创建HttpApplication的事件处理程序
public class ModuleExample : IHttpModule
{
public void Init(System.Web.HttpApplication application)
{
application.PostAuthenticateRequest += (sender, args) =>
{
HttpContext context = ((HttpApplication)sender).Context;
context.Response.Write("请求PostAuthenticate");
};
application.BeginRequest += (sender, args) =>
{
HttpContext context = ((HttpApplication)sender).Context;
context.Response.Write("请求到达");
};
application.EndRequest += (sender, args) =>
{
HttpContext context = ((HttpApplication)sender).Context;
context.Response.Write("请求结束");
};
}
public void Dispose()
{
throw new NotImplementedException();
}
}
2、注册HttpModule
在Asp.net中,实现IHttpModule接口只是实现HttpModule的第一步,在Asp.net中所使用的HttpModule还必须在网站配置文件中进行注册才能真正生效,并在Asp.net中使用。
<system.webServer>
<modules>
<add name="ModuleExample" type="Samples.ModeleExample">
</modules>
</system.webServer>
3、常见的HttpModule
在Asp.net中,已经预定义了许多HttpModule,甚至已经在服务器的网站配置文件中进行了注册,在系统文件夹C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config中看到已经注册的HttpModule如下:
<httpModules>
<add name="OutputCache" type="System.Web.Caching.OutputCacheModule" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" />
<add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
<add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule" />
<add name="RoleManager" type="System.Web.Security.RoleManagerModule" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
<add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" />
<add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule" />
<add name="Profile" type="System.Web.Profile.ProfileModule" />
<add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" />
<add name="ScriptModule-4.0" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>
来源:https://www.cnblogs.com/springsnow/p/9433950.html
猜你喜欢
- 最近几天从网上找了几个asp.net的登录案例想要研究研究代码,结果在用Sql Server2005附加数据库文件时弹出错误信息:看到网友回
- 本文实例讲述了Python中pandas模块DataFrame创建方法。分享给大家供大家参考,具体如下:DataFrame创建1. 通过列表
- 随机漫步是这样行走得到的途径:每次行走都是完全随机的,没有明确的方向,结果是由一系列随机决策决定的。random_walk.py#rando
- np.where共两种用法:第一种np.where(condition, x, y),即condition为条件,当满足条件输出为x,不满足
-   本文介绍基于Python中ArcPy模块,实现基于栅格图像批量裁剪栅格图像,同时对齐各个栅格图像的空
- MySQL有多种存储引擎:MyISAM、InnoDB、MERGE、MEMORY(HEAP)、BDB(BerkeleyDB)、EXAMPLE、
- 本文实例为大家分享了mysql5.7安装图文教程供大家参考,具体内容如下1.在官网下载解压缩版2.解压后配置默认文件新建个my.ini(可以
- 要查看当前是否已开启事件调度器 SHOW VARIABLES LIKE 'event_scheduler'; 开启事件查看器
- 目录1、Unittest为Python内嵌的测试框架,不需要特殊配置2、编写规范总结1、Unittest为Python内嵌的测试框架,不需要
- 这次哀悼,网页设计方面除了应用CSS灰度配色和滤镜,还用到正计时代码,就象汶川大地震已过去了多少天。下面这段代码,是从网易页面提取出来的,具
- JavaScript 变量可以是局部变量或全局变量。私有变量可以用到闭包。全局变量函数可以访问是有函数内部定义的变量,如:实例functio
- 1.在浏览器上搜索PyCharmhttps://www.jetbrains.com/pycharm/download/#section=wi
- 开篇语本文主要是回顾下从项目创建到生成数据到数据库(代码优先)的全部过程。采用EFCore作为ORM框架。本次示例环境:vs2019、net
- PHP观察者模式(Observer Pattern)观察者模式是一种行为设计模式,它定义了一种订阅机制,让一个或多个对象(观察者)自动被通知
- python中自定义模块导入路径的方式主要有以下3种:(1)使用sys.path.append()随着程序执行,会动态地添加模块导入的路径,
- 小整数/* interpreter state */#define _PY_NSMALLPOSINTS &nbs
- 本人机器环境:Windows 2008 R2MySQL 5.6以“Window下忘记Mysql的root密码”百度,找到一大堆解决方案。大多
- 1. 停止服务MySQL2. 卸载mysql相关的程序3. 删除注册表(运行->regedit),machine->system
- 各位大家好!很荣幸能在这里和大家聊聊!(*^__^*) 嘻嘻……此处省略488字,切入正题。关于网页设计这个行业,在中国来讲这个行业并不成熟
- 内容摘要:这篇文章的主旨是弄清楚如何根据实际需求实现一个联动菜单以及联动菜单的原理,实例是实现一个日期选择下拉菜单。首先来分析一下日期下拉菜