Esempio n. 1
0
            public ActionSelectorCacheItem(HttpControllerDescriptor controllerDescriptor)
            {
                Contract.Assert(controllerDescriptor != null);

                // Initialize the cache entirely in the ctor on a single thread.
                _controllerDescriptor = controllerDescriptor;

                MethodInfo[] allMethods   = _controllerDescriptor.ControllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public);
                MethodInfo[] validMethods = Array.FindAll(allMethods, IsValidActionMethod);

                _combinedActionDescriptors = new ReflectedHttpActionDescriptor[validMethods.Length];
                for (int i = 0; i < validMethods.Length; i++)
                {
                    MethodInfo method = validMethods[i];
                    ReflectedHttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(_controllerDescriptor, method);
                    _combinedActionDescriptors[i] = actionDescriptor;
                    HttpActionBinding actionBinding = actionDescriptor.ActionBinding;

                    // Building an action parameter name mapping to compare against the URI parameters coming from the request. Here we only take into account required parameters that are simple types and come from URI.
                    _actionParameterNames.Add(
                        actionDescriptor,
                        actionBinding.ParameterBindings
                        .Where(binding => !binding.Descriptor.IsOptional && TypeHelper.CanConvertFromString(binding.Descriptor.ParameterType) && binding.WillReadUri())
                        .Select(binding => binding.Descriptor.Prefix ?? binding.Descriptor.ParameterName).ToArray());
                }

                if (controllerDescriptor.GetCustomAttributes <IHttpRouteInfoProvider>(inherit: true).Any())
                {
                    // The controller has an attribute route; no actions are accessible via standard routing.
                    _standardActionDescriptors = new ReflectedHttpActionDescriptor[0];
                }
                else
                {
                    // The controller does not have an attribute route; some actions may be accessible via standard
                    // routing.
                    List <ReflectedHttpActionDescriptor> standardActionDescriptors =
                        new List <ReflectedHttpActionDescriptor>();

                    for (int i = 0; i < _combinedActionDescriptors.Length; i++)
                    {
                        ReflectedHttpActionDescriptor actionDescriptor = _combinedActionDescriptors[i];
                        if (!actionDescriptor.GetCustomAttributes <IHttpRouteInfoProvider>(inherit: true).Any())
                        {
                            standardActionDescriptors.Add(actionDescriptor);
                        }
                    }

                    _standardActionDescriptors = standardActionDescriptors.ToArray();
                }

                _combinedActionNameMapping = _combinedActionDescriptors.ToLookup(actionDesc => actionDesc.ActionName, StringComparer.OrdinalIgnoreCase);
                _standardActionNameMapping = _standardActionDescriptors.ToLookup(actionDesc => actionDesc.ActionName, StringComparer.OrdinalIgnoreCase);

                // Bucket the action descriptors by common verbs.
                int len = _cacheListVerbKinds.Length;

                _cacheListVerbs = new ReflectedHttpActionDescriptor[len][];
                for (int i = 0; i < len; i++)
                {
                    _cacheListVerbs[i] = FindActionsForVerbWorker(_cacheListVerbKinds[i]);
                }
            }
            private List <ReflectedHttpActionDescriptor> FindActionUsingRouteAndQueryParameters(HttpControllerContext controllerContext, ReflectedHttpActionDescriptor[] actionsFound, bool hasActionRouteKey)
            {
                IDictionary <string, object> routeValues         = controllerContext.RouteData.Values;
                HashSet <string>             routeParameterNames = new HashSet <string>(routeValues.Keys, StringComparer.OrdinalIgnoreCase);

                routeParameterNames.Remove(ControllerRouteKey);
                if (hasActionRouteKey)
                {
                    routeParameterNames.Remove(ActionRouteKey);
                }

                HttpRequestMessage request = controllerContext.Request;
                Uri  requestUri            = request.RequestUri;
                bool hasQueryParameters    = requestUri != null && !String.IsNullOrEmpty(requestUri.Query);
                bool hasRouteParameters    = routeParameterNames.Count != 0;

                List <ReflectedHttpActionDescriptor> matches = new List <ReflectedHttpActionDescriptor>(actionsFound.Length);

                if (hasRouteParameters || hasQueryParameters)
                {
                    var combinedParameterNames = new HashSet <string>(routeParameterNames, StringComparer.OrdinalIgnoreCase);
                    if (hasQueryParameters)
                    {
                        foreach (var queryNameValuePair in request.GetQueryNameValuePairs())
                        {
                            combinedParameterNames.Add(queryNameValuePair.Key);
                        }
                    }

                    // action parameters is a subset of route parameters and query parameters
                    for (int i = 0; i < actionsFound.Length; i++)
                    {
                        ReflectedHttpActionDescriptor descriptor = actionsFound[i];
                        if (IsSubset(_actionParameterNames[descriptor], combinedParameterNames))
                        {
                            matches.Add(descriptor);
                        }
                    }
                    if (matches.Count > 1)
                    {
                        // select the results that match the most number of required parameters
                        matches = matches
                                  .GroupBy(descriptor => _actionParameterNames[descriptor].Length)
                                  .OrderByDescending(g => g.Key)
                                  .First()
                                  .ToList();
                    }
                }
                else
                {
                    // return actions with no parameters
                    for (int i = 0; i < actionsFound.Length; i++)
                    {
                        ReflectedHttpActionDescriptor descriptor = actionsFound[i];
                        if (_actionParameterNames[descriptor].Length == 0)
                        {
                            matches.Add(descriptor);
                        }
                    }
                }
                return(matches);
            }