/// <summary>
        /// Simulating the action selecting process. It mimics the ASP.NET Web API internal logic
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <returns>A structure contains the log of selecting process</returns>
        public ActionSelectionLog Simulate(HttpControllerContext controllerContext)
        {
            Initialize(controllerContext.ControllerDescriptor);

            ActionSelectionLog log = new ActionSelectionLog(_actionDescriptors);

            // If the action name exists in route data, filter the action descriptors based on action name.
            ReflectedHttpActionDescriptor[] actionsFoundByMethod = null;
            var routeData = controllerContext.Request.GetRouteData();
            string actionName;
            if (routeData.Values.TryGetValue("action", out actionName))
            {
                var actionsFound = _actionNameMapping[actionName].OfType<ReflectedHttpActionDescriptor>().ToArray();

                // Filter actions based on verb.
                actionsFoundByMethod = actionsFound
                    .Where(actionDescriptor => actionDescriptor.SupportedHttpMethods.Contains(controllerContext.Request.Method))
                    .ToArray();

                log.ActionName = actionName;
                log.MarkSelected(actionsFound, info => info.FoundByActionName = true);
                log.MarkOthersSelected(actionsFound, info => info.FoundByActionName = false);

                log.MarkSelected(actionsFound, info => info.FoundByActionNameWithRightVerb = false);
                log.MarkSelected(actionsFoundByMethod, info => info.FoundByActionNameWithRightVerb = true);
            }
            else
            {
                log.ActionName = string.Empty;

                // If action name doesn't exist, find actions based on HTTP verb.
                log.HttpMethod = controllerContext.Request.Method;

                if (string.IsNullOrEmpty(actionName))
                {
                    actionsFoundByMethod = FindActionsForVerb(log.HttpMethod);

                    log.MarkSelected(actionsFoundByMethod, info => info.FoundByVerb = true);
                }
            }

            // If no action is found at this stage a failure must happen.
            if (actionsFoundByMethod != null && actionsFoundByMethod.Length != 0)
            {
                // filter the actions by parameters matching
                var actionsFilterByParam = FindActionUsingRouteAndQueryParameters(
                    controllerContext,
                    actionsFoundByMethod,
                    !string.IsNullOrEmpty(actionName)).ToArray();
                log.MarkSelected(actionsFoundByMethod, info => info.FoundWithRightParam = false);
                log.MarkSelected(actionsFilterByParam, info => info.FoundWithRightParam = true);

                // filter the actions by selection filters
                var actionsFilterBySelectors = RunSelectionFilters(controllerContext, actionsFilterByParam).ToArray();
                log.MarkSelected(actionsFilterByParam, info => info.FoundWithSelectorsRun = false);
                log.MarkSelected(actionsFilterBySelectors, info => info.FoundWithSelectorsRun = true);
            }

            return log;
        }
Пример #2
0
        /// <summary>
        /// Simulating the action selecting process. It mimics the ASP.NET Web API internal logic
        /// </summary>
        /// <param name="controllerContext">The controller context.</param>
        /// <returns>A structure contains the log of selecting process</returns>
        public ActionSelectionLog Simulate(HttpControllerContext controllerContext)
        {
            Initialize(controllerContext.ControllerDescriptor);

            ActionSelectionLog log = new ActionSelectionLog(_actionDescriptors);

            // If the action name exists in route data, filter the action descriptors based on action name.
            ReflectedHttpActionDescriptor[] actionsFoundByMethod = null;
            var    routeData = controllerContext.Request.GetRouteData();
            string actionName;

            if (routeData.Values.TryGetValue("action", out actionName))
            {
                var actionsFound = _actionNameMapping[actionName].OfType <ReflectedHttpActionDescriptor>().ToArray();

                // Filter actions based on verb.
                actionsFoundByMethod = actionsFound
                                       .Where(actionDescriptor => actionDescriptor.SupportedHttpMethods.Contains(controllerContext.Request.Method))
                                       .ToArray();

                log.ActionName = actionName;
                log.MarkSelected(actionsFound, info => info.FoundByActionName       = true);
                log.MarkOthersSelected(actionsFound, info => info.FoundByActionName = false);

                log.MarkSelected(actionsFound, info => info.FoundByActionNameWithRightVerb         = false);
                log.MarkSelected(actionsFoundByMethod, info => info.FoundByActionNameWithRightVerb = true);
            }
            else
            {
                log.ActionName = string.Empty;

                // If action name doesn't exist, find actions based on HTTP verb.
                log.HttpMethod = controllerContext.Request.Method;

                if (string.IsNullOrEmpty(actionName))
                {
                    actionsFoundByMethod = FindActionsForVerb(log.HttpMethod);

                    log.MarkSelected(actionsFoundByMethod, info => info.FoundByVerb = true);
                }
            }

            // If no action is found at this stage a failure must happen.
            if (actionsFoundByMethod != null && actionsFoundByMethod.Length != 0)
            {
                // filter the actions by parameters matching
                var actionsFilterByParam = FindActionUsingRouteAndQueryParameters(
                    controllerContext,
                    actionsFoundByMethod,
                    !string.IsNullOrEmpty(actionName)).ToArray();
                log.MarkSelected(actionsFoundByMethod, info => info.FoundWithRightParam = false);
                log.MarkSelected(actionsFilterByParam, info => info.FoundWithRightParam = true);

                // filter the actions by selection filters
                var actionsFilterBySelectors = RunSelectionFilters(controllerContext, actionsFilterByParam).ToArray();
                log.MarkSelected(actionsFilterByParam, info => info.FoundWithSelectorsRun     = false);
                log.MarkSelected(actionsFilterBySelectors, info => info.FoundWithSelectorsRun = true);
            }

            return(log);
        }