zoukankan      html  css  js  c++  java
  • [Asp.net基础]httpmodule, httphandler, handlerfactory(未完待续, 还有异步处理)

    ASP.NET对请求处理的过程:

    HttpRequest-->inetinfo.exe-->ASPNET_ISAPI.dll-->ASPNET_WP.exe-->HttpRuntime-->HttpApplication Factory-->HttpApplication-->HttpModule-->HttpHandler Factory-->HttpHandler-->HttpHandler.ProcessRequest()

    QQ截图20110614165051

    通过流程图, 我们可以看到, 在asp.net请求管道中, httpmodule处理管道内之前和之后的工作, 中间真正的处理由aspx或者ashx等进行.如前所说,HttpModule会在页面处理前和后对页面进行处理,所以它不会影响真正的页面请求。通常用在给每个页面的头部或者尾部添加一些信息(如版权声明)等. 或者多语言, 权限等等工作.

    新建一个MyHttpModuleMyHttpHandler, 代码如下

    MyHttpModule中的初始化方法中注册以下事件

    public void Init(System.Web.HttpApplication application)
            {
                application.BeginRequest += new EventHandler(application_BeginRequest);
                application.EndRequest += new EventHandler(application_EndRequest);
                application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
                application.PostRequestHandlerExecute += new EventHandler(application_PostRequestHandlerExecute);
                application.ReleaseRequestState += new EventHandler(application_ReleaseRequestState);
                application.AcquireRequestState += new EventHandler(application_AcquireRequestState);
                application.AuthenticateRequest += new EventHandler(application_AuthenticateRequest);
                application.AuthorizeRequest += new EventHandler(application_AuthorizeRequest);
                application.ResolveRequestCache += new EventHandler(application_ResolveRequestCache);
                application.PreSendRequestHeaders += new EventHandler(application_PreSendRequestHeaders);
                application.PreSendRequestContent += new EventHandler(application_PreSendRequestContent);
            }

    每个事件的代入类似如下

    void application_BeginRequest(object sender, EventArgs e)
            {
                HttpApplication application = (HttpApplication)sender;
                application.Context.Response.Write("application_BeginRequest<br/>");
            }
    MyHttpHandler
    public void ProcessRequest(System.Web.HttpContext context)
            {
                context.Response.ContentType = "text/html";
                context.Response.Write("处理所有的请求");
            }
    WebConfig中注册她们
    <httpModules>
            <add name="MyHttpModule" type="TestModule.MyHttpModule,TestModule"/>
          </httpModules>
          <httpHandlers>
            <add verb="*" path="*" type="TestModule.MyHttpHandler,TestModule"/>
          </httpHandlers>
    httpmodule可以注册多个. 管道的处理顺序由注册的先后顺序决定
    命名   httpmodule ,  name:类名, type: 程序集.类名,程序集
          httphandler,  verb:访问方式(*,get,post), path:处理的后缀名(*,*.aspx,*.ashx)
    请求http://localhost:10153/WebForm1.aspx
    输出
    application_BeginRequest
    global_Application_BeginRequest
    application_AuthenticateRequest
    global_Application_AuthenticateRequest
    application_AuthorizeRequest
    application_ResolveRequestCache
    application_AcquireRequestState
    application_PreRequestHandlerExecute
    处理所有的请求application_PostRequestHandlerExecute
    application_ReleaseRequestState
    application_EndRequest
    application_PreSendRequestHeaders
    可见.aspx的请求被我们自己构造的handler处理了. 而httpmodule在处理管道的首尾加上了一些东西. 这时候我们反过头来看看流程图, 就清晰很多了.
     

    Ihttphandlerfactory接口

    接口用于创建和管理处理请求的 HTTP 处理程序。因此,可以创建一个实现 IHttpHandlerFactory 接口的类,然后将该类用作 HTTP 处理程序。

    通过这种方式创建处理程序可以使您更好地控制对 HTTP 请求的处理。使用这种方式可以将 URL 映射到基于一组条件创建不同处理程序的 HTTP 处理程序工厂。例如,通过使用 HTTP 处理程序工厂,可以创建有限数量的 HTTP 处理程序对象,来访问诸如数据库连接等昂贵或有限的资源。然后,可以在以后的请求中重用这些处理程序对象。

    接口方法如下

    名称 说明
    GetHandler 返回实现 IHttpHandler 接口的类的实例。
    ReleaseHandler 使工厂可以重用现有的处理程序实例。
    class HandlerFactory:System.Web.IHttpHandlerFactory
        {
            public System.Web.IHttpHandler GetHandler(System.Web.HttpContext context, string requestType, string url, string pathTranslated)
            {
                IHttpHandler handlerToReturn = null;
                if ("get" == context.Request.RequestType.ToLower())
                    handlerToReturn = new MyHttpHandler();
                else if ("post" == context.Request.RequestType.ToLower())
                    handlerToReturn = new MyHttpHandler2();
                return handlerToReturn;
            }
     
            public void ReleaseHandler(System.Web.IHttpHandler handler)
            {
            }
        }

    修改配置为

    <httpHandlers>
            <add verb="*" path="*.t1,*.t2" type="TestModule.HandlerFactory,TestModule"/>
          </httpHandlers>

    现在访问后缀名为.t1和.t2的url则会由httphandlerfactory来处理, 在GetHandler中决定哪个来处理这个请求.

  • 相关阅读:
    tfrecord
    数据挖掘模型中的IV和WOE详解
    GBDT
    tensorflow笔记 :常用函数说明
    GAN
    牛客挑战赛 39 牛牛与序列 隔板法 容斥 dp
    4.19 省选模拟赛 跳跃 倍增 二分 线段树 建图
    牛客挑战赛39 牛牛的等差数列
    luogu P6224 [BJWC2014]数据 KD-tree 标准板子 重构+二维平面内最近最远距离查询
    牛客挑战赛39 D 牛牛的数学题 NTT FMT FWT
  • 原文地址:https://www.cnblogs.com/jianjialin/p/2080880.html
Copyright © 2011-2022 走看看