ASP.NET本质论阅读----Http请求处理程序

概要:  处理程序 HttpHandler

     处理程序接口:IHttpHandler和IHttpAsyncHandler

补充:  HttpRuntime将请求交给HttpApplication,HttpApplication不直接处理,而交给HttpHandler处理?

      因为请求具有多样性,将请求映射到给HttpHandlerFactory工厂处理

      

       Asp.net会根据请求类型调用匹配的HttpHandlerFactory的GetHandler()方法获取一个IHttpHandler实例,由这个实例来处理请求。

       1.  处理程序工厂,实现接口IHttpHandlerFactory

public interface IHttpHandlerFactory
{
    IHttpHandler  GetHandler(
                HttpContext context
                ,String requestType
                ,string     url
                ,string pathTranslated);
    void ReleaseHandler(IHttpHandler handler);
}
public interface IHttpAsyncHandler :IHttpHandler
{
    IAsyncResult BeginProcessRequest(HttpContext context
                            ,AsyncCallback cb
                            ,Object extraData);
    void EndProcessRequest(IAsyncResult result);
    void ProcessRequest(HttpContext context);
    void IsReusable{get;}
}

IsReusable表示处理程序对象,是否可以被缓存起来

重用HttpHandler:http://www.cnblogs.com/fish-li/archive/2012/01/29/2331477.html

HttpHandler的主要作用是context.Response.Write()响应一类特定的请求,如ashx

  2.  注册处理程序(add,remove ,clear)

  <system.web>

    <httpHandlers>

      <add   verb="GET"   ---//请求的类型,GET,POST

          path="*.vc"  ---//匹配请求的URL

          type="MyHandler.ValidateCodeHandler, MyHandler.dll"   ----处理工厂(处理程序)类型的名称 />

     </httpHandlers>

  </system.web>

    系统的配置文件默认映射处理程序 路径:Microsoft.Net/Framework/v4.0/Config

  3.  一般处理程序

  type="System.Web.UI.SimpleHandlerFactory"   ashx

  4.  MVC处理程序

  请求--路由到Controller---分配到相应的Action 处理----处理后的数据结果Modal---传递到View

  

    MVC 请求处理过程

    1.网站应用程序接收第一次请求-----在Global.asax中,将Route对象添加到RouteTable对象中

    2.实施路由--------UrlRouteModules使用第一个匹配的Route对象创建RouteData参数对象,然后,创建RequestContext对象

    3.创建MVC路由处理对象-------MVCRouteHandler对象创建一个MvcHandler类的对象实例

    4.创建控制器-------MvcHandler对象通过IControllerFactory对象,一般使用DefaultControllerFactory这个实现类创建控制器的对象实例

    5.执行控制器-----------MvcHandler调用控制器的Excute方法

    6.调用Action----------Controller检查被调用的Action方法,执行Action方法

    7.处理返回结果----------Action方法接收用户的输入参数,处理后得到准备显示的数据,返回一个ViewResult类型输出

  5.禁止的处理程序

    配置 type="System.Web.HttpForbiddenHandler"  Validate="True"  path="*.cs"

    实例:禁止访问Excel、

    <add path="*.xls" verb="*"  type="System.Web.HttpForbiddenHandler"  Validate="True"  />

posted @ 2012-12-31 17:21  RyanRuan  阅读(304)  评论(0编辑  收藏  举报
View Code