Example #1
0
 public RuleConstraintValueViewModel(RuleConstraintValue ruleConstraintValue, IEnumerable <RuleConstraintName> ruleConstraintNames, ICaptionCommand removeConstraintCommand)
 {
     _model = ruleConstraintValue;
     _ruleConstraintNames     = ruleConstraintNames;
     _removeConstraintCommand = removeConstraintCommand;
     Values = ParameterSources.GetParameterSource(Left);
 }
 /// <summary>
 /// Checks if type is supported by model binder instance.
 /// </summary>
 /// <param name="type">The type to check.</param>
 /// <param name="source"></param>
 /// <returns>
 ///   <c>true</c> is parameter is supported, otherwise <c>false</c>
 /// </returns>
 public bool DoesSupportType(Type type, ParameterSources source)
 {
     if (source == ParameterSources.Query || source == ParameterSources.Header)
     {
         foreach (var b in _queryBinders)
         {
             if (b.DoesSupportType(type))
             {
                 return(true);
             }
         }
         return(false);
     }
     if (source == ParameterSources.Body)
     {
         return(true);
     }
     throw new Exception("Error in ModelBinderCollection.DoesSupportType Parameter source needs to be set to Body, Query or Header");
 }
Example #3
0
        /// <summary>
        /// Gets the action parameters metadata for the given action context.
        /// </summary>
        /// <param name="actionCtx">The action context.</param>
        /// <returns>Array of <see cref="ActionParameter"/> retrieved from action context.</returns>
        public virtual ActionParameter[] GetParameters(ActionContext actionCtx)
        {
            var methodParams = actionCtx.Method.GetParameters();

            ActionParameter[] parameters = new ActionParameter[methodParams.Length];
            for (int i = 0; i < methodParams.Length; i++)
            {
                var    param         = actionCtx.Method.GetParameters()[i];
                bool   isFromQuery   = false;
                bool   isFromBody    = false;
                bool   isFromRoute   = false;
                bool   isFromService = false;
                bool   isFromHeader  = false;
                string overridenName = null;

                // todo: refactor with attribute inheritance
                if (param.GetCustomAttribute <FromServicesAttribute>() != null)
                {
                    isFromService = true;
                }
                else
                {
                    isFromQuery = param.GetCustomAttribute <FromQueryAttribute>() != null;
                    isFromBody  = param.GetCustomAttribute <FromBodyAttribute>() != null;
                    isFromRoute = param.GetCustomAttribute <FromRouteAttribute>() != null;
                    var headerAttrib = param.GetCustomAttribute <FromHeaderAttribute>();
                    if (headerAttrib != null)
                    {
                        isFromHeader  = true;
                        overridenName = headerAttrib.HeaderName;
                    }
                }

                ParameterSources source = ParameterSources.Unknown;

                if (isFromService)
                {
                    source = ParameterSources.Service;
                }
                else if (isFromHeader)
                {
                    source = ParameterSources.Header;
                }
                else if (isFromQuery && !isFromBody && !isFromRoute)
                {
                    source = ParameterSources.Query;
                }
                else if (!isFromQuery && isFromBody && !isFromRoute)
                {
                    source = ParameterSources.Body;
                }
                else if (!isFromQuery && !isFromBody && isFromRoute)
                {
                    source = ParameterSources.RouteSegment;
                }


                parameters[i] = new ActionParameter(actionCtx, new ModelBinders.ModelBinderCollection(new JsonSerializer(), _services, _optionsRetriever))
                {
                    Name            = param.Name.ToLower(),
                    DefaultValue    = param.DefaultValue,
                    HasDefaultValue = param.HasDefaultValue,
                    Type            = param.ParameterType,
                    ParameterSource = source,
                    OverridenName   = overridenName
                };

                if (parameters[i].ParameterSource == ParameterSources.Unknown)
                {
                    if (parameters[i].IsComplex)
                    {
                        parameters[i].ParameterSource = ParameterSources.Body;
                    }
                    else if (actionCtx.RouteSegments.Any(x => x.IsParameter && x.ParameterName == parameters[i].Name))
                    {
                        parameters[i].ParameterSource = ParameterSources.RouteSegment;
                    }
                    else
                    {
                        parameters[i].ParameterSource = ParameterSources.Query;
                    }
                }
            }

            return(parameters);
        }
 public void RegisterParameterSource(string parameterName, Func <IEnumerable <string> > action)
 {
     ParameterSources.Add(parameterName, action);
 }