ASP.NET页面生命周期之HttpHandler

单个handler

namespace TestForWeb
{
    public class MyHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Write("hehe");
        }
    }
}

handler工厂

namespace TestForWeb
{
    public class MyHandlerFactory:IHttpHandlerFactory
    {
        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            if (url.Contains("1"))
            {
                return new Handler1();
            }
            else
            {
                return new Handler2();
            }
        }

        public void ReleaseHandler(IHttpHandler handler)
        {
          
        }
    }
    public class Handler1:IHttpHandler
    {

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Write("1");
        }
    }
    public class Handler2 : IHttpHandler
    {

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Write("2");
        }
    }
}

集成模式才会这么用

 <system.webServer>
    <modules>
      <add name="MyModule" type="TestForWeb.MyModule,TestForWeb" />
    </modules>
    <handlers>
      <add name="MyHandlerFactory" path="*.ds" verb="*" type="TestForWeb.MyHandlerFactory,TestForWeb"  />
    </handlers>
  </system.webServer>

 

posted on 2017-05-05 16:07  奔游浪子  阅读(107)  评论(0)    收藏  举报

导航