private static ActionConstraintContext CreateActionConstraintContext(HttpMethodConstraint constraint)
        {
            var context = new ActionConstraintContext();

            var actionSelectorCandidate = new ActionSelectorCandidate(new ActionDescriptor(), new List<IActionConstraint> { constraint });

            context.Candidates = new List<ActionSelectorCandidate> { actionSelectorCandidate };
            context.CurrentCandidate = context.Candidates[0];

            return context;
        }
예제 #2
0
        private List <OverloadedParameter> GetOverloadableParameters(ActionSelectorCandidate candidate)
        {
            if (candidate.Action.Parameters == null)
            {
                return(null);
            }

            var isOverloaded = false;

            foreach (var constraint in candidate.Constraints)
            {
                if (constraint is OverloadActionConstraint)
                {
                    isOverloaded = true;
                }
            }

            if (!isOverloaded)
            {
                return(null);
            }

            var parameters = new List <OverloadedParameter>();

            foreach (var parameter in candidate.Action.Parameters)
            {
                // We only consider parameters that are bound from the URL.
                if ((parameter.BinderMetadata is IRouteDataValueProviderMetadata ||
                     parameter.BinderMetadata is IQueryValueProviderMetadata) &&
                    ValueProviderResult.CanConvertFromString(parameter.ParameterType))
                {
                    var optionalMetadata = parameter.BinderMetadata as IOptionalBinderMetadata;
                    if (optionalMetadata == null || optionalMetadata.IsOptional)
                    {
                        // Optional parameters are ignored in overloading. If a parameter doesn't specify that it's
                        // required then treat it as optional (MVC default). WebAPI parameters will all by-default
                        // specify themselves as required unless they have a default value.
                        continue;
                    }

                    var nameProvider = parameter.BinderMetadata as IModelNameProvider;
                    var prefix       = nameProvider?.Name ?? parameter.Name;

                    parameters.Add(new OverloadedParameter()
                    {
                        ParameterDescriptor = parameter,
                        Prefix = prefix,
                    });
                }
            }

            return(parameters);
        }
예제 #3
0
        private List <OverloadedParameter> GetOverloadableParameters(ActionSelectorCandidate candidate)
        {
            if (candidate.Action.Parameters == null)
            {
                return(null);
            }

            var isOverloaded = false;

            foreach (var constraint in candidate.Constraints)
            {
                if (constraint is OverloadActionConstraint)
                {
                    isOverloaded = true;
                }
            }

            if (!isOverloaded)
            {
                return(null);
            }

            var parameters = new List <OverloadedParameter>();

            foreach (var parameter in candidate.Action.Parameters)
            {
                // We only consider parameters that are bound from the URL.
                if ((parameter.BinderMetadata is IRouteDataValueProviderMetadata ||
                     parameter.BinderMetadata is IQueryValueProviderMetadata) &&
                    !parameter.IsOptional &&
                    ValueProviderResult.CanConvertFromString(parameter.ParameterType))
                {
                    var nameProvider = parameter.BinderMetadata as IModelNameProvider;
                    var prefix       = nameProvider?.Name ?? parameter.Name;

                    parameters.Add(new OverloadedParameter()
                    {
                        ParameterDescriptor = parameter,
                        Prefix = prefix,
                    });
                }
            }

            return(parameters);
        }
        private List<OverloadedParameter> GetOverloadableParameters(ActionSelectorCandidate candidate)
        {
            if (candidate.Action.Parameters == null)
            {
                return null;
            }

            var isOverloaded = false;
            foreach (var constraint in candidate.Constraints)
            {
                if (constraint is OverloadActionConstraint)
                {
                    isOverloaded = true;
                }
            }

            if (!isOverloaded)
            {
                return null;
            }

            var parameters = new List<OverloadedParameter>();
            object optionalParametersObject;
            candidate.Action.Properties.TryGetValue("OptionalParameters", out optionalParametersObject);
            var optionalParameters = (HashSet<string>)optionalParametersObject;
            foreach (var parameter in candidate.Action.Parameters)
            {
                // We only consider parameters that are marked as bound from the URL.
                var source = parameter.BindingInfo?.BindingSource;
                if (source == null)
                {
                    continue;
                }

                if ((source.CanAcceptDataFrom(BindingSource.Path) ||
                    source.CanAcceptDataFrom(BindingSource.Query)) &&
                    ValueProviderResult.CanConvertFromString(parameter.ParameterType))
                {
                    if (optionalParameters != null)
                    {
                        var isOptional = optionalParameters.Contains(parameter.Name);
                        if (isOptional)
                        {
                            // Optional parameters are ignored in overloading. If a parameter doesn't specify that it's
                            // required then treat it as optional (MVC default). WebAPI parameters will all by-default
                            // specify themselves as required unless they have a default value.
                            continue;
                        }
                    }

                    var prefix = parameter.BindingInfo?.BinderModelName ?? parameter.Name;

                    parameters.Add(new OverloadedParameter()
                    {
                        ParameterDescriptor = parameter,
                        Prefix = prefix,
                    });
                }
            }

            return parameters;
        }