예제 #1
0
        public static HttpControllerDescriptor GetDirectRouteController(this IHttpRoute route)
        {
            Contract.Assert(route != null);

            ReflectedHttpActionDescriptor[] directRouteActions = route.GetDirectRouteActions();
            if (directRouteActions != null)
            {
                // Set the controller descriptor for the first action descriptor
                Contract.Assert(directRouteActions.Length > 0);
                HttpControllerDescriptor controllerDescriptor = directRouteActions[0].ControllerDescriptor;

                // Check that all other action descriptors share the same controller descriptor
                for (int i = 1; i < directRouteActions.Length; i++)
                {
                    if (directRouteActions[i].ControllerDescriptor != controllerDescriptor)
                    {
                        return(null);
                    }
                }

                return(controllerDescriptor);
            }

            return(null);
        }
예제 #2
0
            private ReflectedHttpActionDescriptor[] GetInitialCandidateList(HttpControllerContext controllerContext, bool ignoreVerbs = false)
            {
                IHttpRouteData routeData = controllerContext.RouteData;

                IHttpRoute route = routeData.Route;

                ReflectedHttpActionDescriptor[] actions;
                if (route != null)
                {
                    actions = route.GetDirectRouteActions();
                    if (actions != null)
                    {
                        return(actions);
                    }
                }

                HttpMethod incomingMethod = controllerContext.Request.Method;

                string actionName;

                if (routeData.Values.TryGetValue(RouteKeys.ActionKey, out actionName))
                {
                    // We have an explicit {action} value, do traditional binding. Just lookup by actionName
                    ReflectedHttpActionDescriptor[] actionsFoundByName = _actionNameMapping[actionName].ToArray();

                    // Throws HttpResponseException with NotFound status because no action matches the Name
                    if (actionsFoundByName.Length == 0)
                    {
                        throw new HttpResponseException(controllerContext.Request.CreateErrorResponse(
                                                            HttpStatusCode.NotFound,
                                                            Error.Format(SRResources.ResourceNotFound, controllerContext.Request.RequestUri),
                                                            Error.Format(SRResources.ApiControllerActionSelector_ActionNameNotFound, _controllerDescriptor.ControllerName, actionName)));
                    }

                    if (ignoreVerbs)
                    {
                        actions = actionsFoundByName;
                    }
                    else
                    {
                        actions = FilterIncompatibleVerbs(incomingMethod, actionsFoundByName);
                    }
                }
                else
                {
                    if (ignoreVerbs)
                    {
                        actions = _actionDescriptors;
                    }
                    else
                    {
                        // No direct routing or {action} parameter, infer it from the verb.
                        actions = FindActionsForVerb(incomingMethod);
                    }
                }

                return(actions);
            }
 // If route is a direct route, get the http method for its actions.
 // Else return null.
 public static HttpMethod GetDirectRouteVerb(this IHttpRoute route)
 {
     ReflectedHttpActionDescriptor[] ads = route.GetDirectRouteActions();
     if (ads != null)
     {
         // All action descriptors on this route have the same method, so just pull the first.
         return(ads[0].SupportedHttpMethods[0]);
     }
     return(null);
 }