Http请求处理流程 管道流程 MVC扩展HttpModule
HttpApplication 封装了管道处理请求的所有事件
HttpModule 对HttpApplication中事件的扩展
HttpHandler 处理程序 每个请求都要经过Handler处理
HttpContext 容器 保存了请求的所有信息
请求进来 先直接内置的所有管道事件也就是所有的HttpModule 然后执行HttpHandler 这个就是自己对请求进行的操作 自己写的代码
HttpHandler和HttpModule都可以处理http请求 HttpModule的作用类似AOP,是针对某些通用功能(请求拦截、身份认证、检查功能)的,而HttpHandler常用来处理某一类(ashx、aspx、asmx)http请求
EntityFramework 版本
扩展HttpModule
HttpModule是每个请求都会执行到的
1.创建一个类 继承IHttpModule 扩展HttpModuleModule
public class MyCustomModule : IHttpModule { /// <summary> /// 您将需要在网站的 Web.config 文件中配置此模块 /// 并向 IIS 注册它,然后才能使用它。有关详细信息, /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007 /// </summary> #region IHttpModule Members public void Dispose() { //此处放置清除代码。 } public void Init(HttpApplication application) { application.AcquireRequestState += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "AcquireRequestState ")); application.AuthenticateRequest += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "AuthenticateRequest ")); application.AuthorizeRequest += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "AuthorizeRequest ")); application.BeginRequest += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "BeginRequest ")); application.Disposed += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "Disposed ")); application.EndRequest += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "EndRequest ")); application.Error += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "Error ")); application.LogRequest += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "LogRequest ")); application.MapRequestHandler += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "MapRequestHandler ")); application.PostAcquireRequestState += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "PostAcquireRequestState ")); application.PostAuthenticateRequest += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "PostAuthenticateRequest ")); application.PostAuthorizeRequest += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "PostAuthorizeRequest ")); application.PostLogRequest += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "PostLogRequest ")); application.PostMapRequestHandler += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "PostMapRequestHandler ")); application.PostReleaseRequestState += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "PostReleaseRequestState ")); application.PostRequestHandlerExecute += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "PostRequestHandlerExecute ")); application.PostResolveRequestCache += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "PostResolveRequestCache ")); application.PostUpdateRequestCache += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "PostUpdateRequestCache ")); application.PreRequestHandlerExecute += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "PreRequestHandlerExecute ")); application.PreSendRequestContent += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "PreSendRequestContent ")); application.PreSendRequestHeaders += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "PreSendRequestHeaders ")); application.ReleaseRequestState += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "ReleaseRequestState ")); application.RequestCompleted += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "RequestCompleted ")); application.ResolveRequestCache += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "ResolveRequestCache ")); application.UpdateRequestCache += (s, e) => application.Response.Write(string.Format("<h1 style='color:#00f'>来自MyCustomModule 的处理,{0}请求到达 {1}</h1><hr>", DateTime.Now.ToString(), "UpdateRequestCache ")); //context.AuthenticateRequest //验证请求,一般用来取得请求用户的信息 //context.PostAuthenticateRequest 已经获取请求用户的信息 //context.AuthorizeRequest 授权,一般用来检查用户的请求是否获得权限 //context.PostAuthorizeRequest 用户请求已经得到授权 //context.ResolveRequestCache 获取以前处理缓存的处理结果,如果以前缓存过,那么,不必再进行请求的处理工作,直接返回缓存结果 //context.PostResolveRequestCache 已经完成缓存的获取操作 //context.PostMapRequestHandler 已经根据用户的请求,创建了处理请求的处理器对象 //context.AcquireRequestState 取得请求的状态,一般用于Session //context.PostAcquireRequestState 已经取得了Session //context.PreRequestHandlerExecute 准备执行处理程序 //context.PostRequestHandlerExecute 已经执行了处理程序 //context.ReleaseRequestState 释放请求的状态 //context.PostReleaseRequestState 已经释放了请求的状态 //context.UpdateRequestCache 更新缓存 //context.PostUpdateRequestCache 已经更新了缓存 //context.LogRequest 请求的日志操作 //context.PostLogRequest 已经完成了请求的日志操作 } #endregion }
我们自己的Controller中写的Action就是在PreRequestHandlerExecute事件跟PostRequestHandlerExecute时间之间执行
Pre刚到这个事件 Post当前事件执行完毕
在这里会通过MvcRouteHandler找到一个执行当前请求的Handler 源码中在System.Web.MVC找MvcRouteHandler
2.写入配置文件Web.config
在system.webServer 标签中 的Modules里
<modules runAllManagedModulesForAllRequests="true"> <add name="MyCustomerModule" type="命名空间+类名,程序集名"/> </modules>
3.框架默认有一套HttpModule 如果知道不需要其中某些HttpModule 移除指定的HttpModule
<modules runAllManagedModulesForAllRequests="false"> <remove name="FormsAuthentication" /> <remove name="WindowsAuthentication" /> <remove name="PassportAuthentication" /> <remove name="RoleManager" /> <remove name="FileAuthorization" /> <remove name="UrlAuthorization" /> </modules>
***************
之前版本的Asp.Net MVC正是通过 UrlRoutingModule.cs 类和 MvcHandler.cs 类进行扩展从而实现了MVC框架。
**************************
而在Asp.Net Core里面,管道模型流程发生了很大的变化:
IHttpModule和IHttpHandler不复存在,取而代之的是一个个中间件(Middleware)。
*********ASP.NET管道和.NET Core管道区别
http://www.cnblogs.com/niklai/p/5665272.html
**********MVC源码学习
https://www.cnblogs.com/landeanfen/p/5989092.html