哪个HTTP模块调用ASP.NET MVC Action Filters?

时间:2015-02-20 06:22:38

标签: asp.net asp.net-mvc

哪个ASP.NET HttpModule负责调用在ASP.NET MVC应用程序中注册的操作过滤器?

1 个答案:

答案 0 :(得分:1)

似乎不是由HttpModule完成的,而是由System.Web.Mvc.ControllerActionInvoker类完成的,由Controller类用于调用操作方法。 有ControllerActionInvoker.InvokeAction方法的反编译代码,调用过滤器方法

public virtual bool InvokeAction(ControllerContext controllerContext, string actionName)
    {
        if (controllerContext == null)
        {
            throw new ArgumentNullException("controllerContext");
        }
        if (string.IsNullOrEmpty(actionName) && !controllerContext.RouteData.HasDirectRouteMatch())
        {
            throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
        }
        ControllerDescriptor controllerDescriptor = this.GetControllerDescriptor(controllerContext);
        ActionDescriptor actionDescriptor = this.FindAction(controllerContext, controllerDescriptor, actionName);
        if (actionDescriptor != null)
        {
            FilterInfo filters = this.GetFilters(controllerContext, actionDescriptor);
            try
            {
                AuthenticationContext authenticationContext = this.InvokeAuthenticationFilters(controllerContext, filters.AuthenticationFilters, actionDescriptor);
                if (authenticationContext.Result != null)
                {
                    AuthenticationChallengeContext authenticationChallengeContext = this.InvokeAuthenticationFiltersChallenge(controllerContext, filters.AuthenticationFilters, actionDescriptor, authenticationContext.Result);
                    this.InvokeActionResult(controllerContext, authenticationChallengeContext.Result ?? authenticationContext.Result);
                }
                else
                {
                    AuthorizationContext authorizationContext = this.InvokeAuthorizationFilters(controllerContext, filters.AuthorizationFilters, actionDescriptor);
                    if (authorizationContext.Result != null)
                    {
                        AuthenticationChallengeContext authenticationChallengeContext2 = this.InvokeAuthenticationFiltersChallenge(controllerContext, filters.AuthenticationFilters, actionDescriptor, authorizationContext.Result);
                        this.InvokeActionResult(controllerContext, authenticationChallengeContext2.Result ?? authorizationContext.Result);
                    }
                    else
                    {
                        if (controllerContext.Controller.ValidateRequest)
                        {
                            ControllerActionInvoker.ValidateRequest(controllerContext);
                        }
                        IDictionary<string, object> parameterValues = this.GetParameterValues(controllerContext, actionDescriptor);
                        ActionExecutedContext actionExecutedContext = this.InvokeActionMethodWithFilters(controllerContext, filters.ActionFilters, actionDescriptor, parameterValues);
                        AuthenticationChallengeContext authenticationChallengeContext3 = this.InvokeAuthenticationFiltersChallenge(controllerContext, filters.AuthenticationFilters, actionDescriptor, actionExecutedContext.Result);
                        this.InvokeActionResultWithFilters(controllerContext, filters.ResultFilters, authenticationChallengeContext3.Result ?? actionExecutedContext.Result);
                    }
                }
            }
            catch (ThreadAbortException)
            {
                throw;
            }
            catch (Exception exception)
            {
                ExceptionContext exceptionContext = this.InvokeExceptionFilters(controllerContext, filters.ExceptionFilters, exception);
                if (!exceptionContext.ExceptionHandled)
                {
                    throw;
                }
                this.InvokeActionResult(controllerContext, exceptionContext.Result);
            }
            return true;
        }
        return false;
    }