服务器之家

服务器之家 > 正文

ASP.NET MVC 从IHttp到页面输出的实例代码

时间:2019-11-17 13:16     来源/作者:asp.net教程网

代码如下:


MVCHandler : IHttpHandler
void IHttpHandler.ProcessRequest(HttpContext httpContext)
{
    this.ProcessRequest(httpContext);
}

 

protected virtual void ProcessRequest(HttpContext httpContext)
{
    HttpContextBase base2 = new HttpContextWrapper(httpContext);
    this.ProcessRequest(base2);
}

protected internal virtual void ProcessRequest(HttpContextBase httpContext)
{
    IController controller;
    IControllerFactory factory;
    this.ProcessRequestInit(httpContext, out controller, out factory);
    try
    {
        controller.Execute(this.RequestContext);
    }
    finally
    {
        factory.ReleaseController(controller);
    }
}        

 

 

复制代码代码如下:


Controller : ControllerBase : IController
void IController.Execute(RequestContext requestContext)  //------>Controller
{
    this.Execute(requestContext);
}

 

protected virtual void Execute(RequestContext requestContext)   //------>ControllerBase
{
    if (requestContext == null)
    {
        throw new ArgumentNullException("requestContext");
    }
    if (requestContext.HttpContext == null)
    {
        throw new ArgumentException(MvcResources.ControllerBase_CannotExecuteWithNullHttpContext, "requestContext");
    }
    this.VerifyExecuteCalledOnce();
    this.Initialize(requestContext);
    using (ScopeStorage.CreateTransientScope())
    {
        this.ExecuteCore();
    }
}

protected override void ExecuteCore()   //------>Controller
{
    this.PossiblyLoadTempData();
    try
    {
        string requiredString = this.RouteData.GetRequiredString("action");
        if (!this.ActionInvoker.InvokeAction(base.ControllerContext, requiredString))   //public IActionInvoker ActionInvoker { get; set; }
        {
            this.HandleUnknownAction(requiredString);
        }
    }
    finally
    {
        this.PossiblySaveTempData();
    }
}

 

 

复制代码代码如下:


ControllerActionInvoker : IActionInvoker
public virtual bool InvokeAction(ControllerContext controllerContext, string actionName)
{
    if (controllerContext == null)
    {
        throw new ArgumentNullException("controllerContext");
    }
    if (string.IsNullOrEmpty(actionName))
    {
        throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
    }
    ControllerDescriptor controllerDescriptor = this.GetControllerDescriptor(controllerContext);
    ActionDescriptor actionDescriptor = this.FindAction(controllerContext, controllerDescriptor, actionName);
    if (actionDescriptor == null)
    {
        return false;
    }
    FilterInfo filters = this.GetFilters(controllerContext, actionDescriptor);
    try
    {
        AuthorizationContext context = this.InvokeAuthorizationFilters(controllerContext, filters.AuthorizationFilters, actionDescriptor);
        if (context.Result != null)
        {
            this.InvokeActionResult(controllerContext, context.Result);
        }
        else
        {
            if (controllerContext.Controller.ValidateRequest)
            {
                ValidateRequest(controllerContext);
            }
            IDictionary<string, object> parameterValues = this.GetParameterValues(controllerContext, actionDescriptor);
            ActionExecutedContext context2 = this.InvokeActionMethodWithFilters(controllerContext, filters.ActionFilters, actionDescriptor, parameterValues);
            this.InvokeActionResultWithFilters(controllerContext, filters.ResultFilters, context2.Result);
        }
    }
    catch (ThreadAbortException)
    {
        throw;
    }
    catch (Exception exception)
    {
        ExceptionContext context3 = this.InvokeExceptionFilters(controllerContext, filters.ExceptionFilters, exception);
        if (!context3.ExceptionHandled)
        {
            throw;
        }
        this.InvokeActionResult(controllerContext, context3.Result);
    }
    return true;
}

 

protected virtual void InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
{
    actionResult.ExecuteResult(controllerContext);
}

 

 

复制代码代码如下:


ActionResult
public override void ExecuteResult(ControllerContext context)
{
    if (context == null)
    {
        throw new ArgumentNullException("context");
    }
    if (string.IsNullOrEmpty(this.ViewName))
    {
        this.ViewName = context.RouteData.GetRequiredString("action");
    }
    ViewEngineResult result = null;
    if (this.View == null)
    {
        result = this.FindView(context);   //ViewEngineResult
        this.View = result.View;    //IView接口    
    }
    TextWriter output = context.HttpContext.Response.Output;
    ViewContext viewContext = new ViewContext(context, this.View, this.ViewData, this.TempData, output);
    this.View.Render(viewContext, output);
    if (result != null)
    {
        result.ViewEngine.ReleaseView(context, this.View);
    }
}

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
运维必须知道的关于云服务器的十个问题
运维必须知道的关于云服务器的十个问题 2019-05-24
返回顶部