ASP.NET Framework處理一個Http Request的流程:html
HttpRequest-->inetinfo.exe-->ASPNET_ISAPI.dll-->ASPNET_WP.exe-->HttpRuntime-->HttpApplication Factory-->HttpApplication-->HttpModule-->HttpHandler Factory-->HttpHandler-->HttpHandler.ProcessRequest()web
參考:https://www.cnblogs.com/sunliyuan/p/5876253.html post
參考:https://www.cnblogs.com/firstyi/archive/2008/05/07/1187274.htmlui
實現自定義的HandlerFactoryurl
public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { PageHandlerFactory factory = (PageHandlerFactory)Activator.CreateInstance(typeof(PageHandlerFactory), true); IHttpHandler handler = factory.GetHandler(context, requestType, url, pathTranslated); //執行一些其它操做 Execute(handler); return handler; }
web.config 配置以下:spa
<system.web>.net
<httpHandlers>
<add verb="*" path="*.aspx" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
</httpHandlers>code
</system.web>orm
可是在新版本vs會有託管管道不適用的報錯。htm
檢測到在集成的託管管道模式下不適用的 ASP.NET 設置的解決方法
請參考該文章:https://www.cnblogs.com/wanglg/p/3515765.html
改動點: <system.web> <httpHandlers> <add verb="*" path="*.aspx" type="HttpHandle.MyHandler, HttpHandle"/> </httpHandlers> </system.web> 改成: <system.webServer> <handlers> <add name="MyHandlerFactory" verb="*" path="*.aspx" type="WebForm.Http.MyHandlerFactory, WebForm.Http"/> </handlers> </system.webServer>
以上是針對aspx。
ashx頁面要對factory類稍做改動。
public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { //一些攔截處理 var a = BuildManager.CreateInstanceFromVirtualPath(context.Request.RawUrl, typeof(IHttpHandler)); return a as IHttpHandler; }
經過這個類咱們能夠更加了解.net對http的處理流程,也能夠擴展出許多更有趣的實現。例如在這裏對aspx、ashx頁面注入一些方法^_^