代码改变世界

asp.net mvc source(1)- MvcHandler

2009-07-06 14:04  蜡笔小旧  阅读(492)  评论(0)    收藏  举报
public class MvcHandler : IHttpHandler, IRequiresSessionState {}

public class MvcHttpHandler : UrlRoutingHandler, IRequiresSessionState {}

public class MvcRouteHandler : IRouteHandler {}
 
------------------------------------------------------------------------------------------------------
官方解释
IHttpHandler:
定义 ASP.NET 为使用自定义 HTTP 处理程序同步处理 HTTP Web 请求而实现的协定。
IRequiresSessionState :
指定目标 HTTP 处理程序需要对会话状态值具有读写访问权。这是一个标记接口,没有任何方法。
在自定义 HTTP 处理程序中实现 IRequiresSessionState 接口,以确定处理程序是否需要对会话状态值具有读写访问权。
如果您的处理程序将访问会话状态值,它必须实现 IRequiresSessionState 接口(不包含任何方法的标记接口)。
IRouteHandler :
定义类必须实现才能处理匹配路由模式的请求的协定。 
UrlRoutingHandler:
用作使您能够自定义 ASP.NET 路由如何处理请求的类的基类。 
public abstract class UrlRoutingHandler : IHttpHandler
------------------------------------------------------------------------------------------------------
 
Mvc\MvcHandler.cs
=》(1)

void IHttpHandler.ProcessRequest(HttpContext httpContext) {
    ProcessRequest(httpContext);
}

=>(2)

protected virtual void ProcessRequest(HttpContext httpContext) {
    HttpContextBase iHttpContext = new HttpContextWrapper(httpContext);////通过httpContext上下文,初始化一个MVC环境中的上下文
    ProcessRequest(iHttpContext);//有点像适配器模式
}

=>(3)真正的处理方法

protected internal virtual void ProcessRequest(HttpContextBase httpContext) {
    AddVersionHeader(httpContext);

    // Get the controller type
    string controllerName = RequestContext.RouteData.GetRequiredString("controller");

    // Instantiate the controller and call Execute
    IControllerFactory factory = ControllerBuilder.GetControllerFactory();
    IController controller = factory.CreateController(RequestContext, controllerName);
    if (controller == null) {
        throw new InvalidOperationException(
            String.Format(
                CultureInfo.CurrentUICulture,
                MvcResources.ControllerBuilder_FactoryReturnedNull,
                factory.GetType(),
                controllerName));
    }
    try {
        controller.Execute(RequestContext);
    }
    finally {
        factory.ReleaseController(controller);
    }
}

ControllerBase.cs

=>(4)

#region IController Members
void IController.Execute(RequestContext requestContext) {
    Execute(requestContext);
}
#endregion

 

=>(5)

protected virtual void Execute(RequestContext requestContext) {
    if (requestContext == null) {
        throw new ArgumentNullException("requestContext");
    }

    Initialize(requestContext);
    ExecuteCore();
}

 

=>(6) 初始化上下文,执行交给子类处理

protected abstract void ExecuteCore();

protected virtual void Initialize(RequestContext requestContext) {
    ControllerContext = new ControllerContext(requestContext, this);
}

Controller.cs

=>(7)唯一处理,调用Action

protected override void ExecuteCore() {
     TempData.Load(ControllerContext, TempDataProvider);

     try {
         string actionName = RouteData.GetRequiredString("action");
         if (!ActionInvoker.InvokeAction(ControllerContext, actionName)) {
             HandleUnknownAction(actionName);
         }
     }
     finally {
         TempData.Save(ControllerContext, TempDataProvider);
     }
}

ControllerActionInvoker.cs

=>(8)条用Action

public virtual bool InvokeAction(ControllerContext controllerContext, string actionName) {

}

Controller.cs

=>(9)处理未知的Action 抛404错误

protected virtual void HandleUnknownAction(string actionName) {
    throw new HttpException(404, String.Format(CultureInfo.CurrentUICulture,
        MvcResources.Controller_UnknownAction, actionName, GetType().FullName));
}