zoukankan      html  css  js  c++  java
  • asp.net web 项目 针对aspx和ashx的 IHttpHandlerFactory 开发

    ASP.NET Framework处理一个Http Request的流程:

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

    参考:https://www.cnblogs.com/sunliyuan/p/5876253.html  

    参考:https://www.cnblogs.com/firstyi/archive/2008/05/07/1187274.html

    实现自定义的HandlerFactory

    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 配置如下:

    <system.web>

    <httpHandlers>
          <add verb="*" path="*.aspx" type="HttpHandle.MyHandlerFactory, HttpHandle"/>
    </httpHandlers>

    </system.web>

    但是在新版本vs会有托管管道不适用的报错。

    检测到在集成的托管管道模式下不适用的 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页面注入一些方法^_^

  • 相关阅读:
    SQLAlchemy Table(表)类方式
    MySQL简单入门
    第四次作业
    第三次随笔
    第二次随笔
    第一次随笔
    第四次随笔
    第三次作业
    第二次随笔
    第一次随笔
  • 原文地址:https://www.cnblogs.com/heweiquan123/p/9104997.html
Copyright © 2011-2022 走看看