protected virtual UrlSegment GetSegment(string segment, List <ActionParameter> actionParameters)
        {
            var parameter = actionParameters.FirstOrDefault(p => p.Name.EqualsUncase(segment));

            if (parameter == null)
            {
                return(new UrlSegment(segment));
            }

            var isWildcard   = parameter.HasAttributes <WildcardAttribute, ParamArrayAttribute>();
            var urlParameter = new UrlParameter(parameter, isWildcard);

            return(new UrlSegment(urlParameter, _constraintBuilder.Build(urlParameter)));
        }
예제 #2
0
        public List <string> Build(UrlParameter parameter)
        {
            var constraints = new List <string>();

            if (parameter.TypeDescriptor.Type == typeof(string))
            {
                if (parameter.HasAttribute <AlphaAttribute>())
                {
                    constraints.Add("alpha");
                }

                if (parameter.HasAttribute <LengthAttribute>())
                {
                    var length = parameter.GetAttribute <LengthAttribute>();
                    if (length.Length.HasValue)
                    {
                        constraints.Add($"length({length.Length})");
                    }
                    else if (length.Min.HasValue && length.Max.HasValue)
                    {
                        constraints.Add($"length({length.Min},{length.Max})");
                    }
                    else if (length.Min.HasValue)
                    {
                        constraints.Add($"minlength({length.Min})");
                    }
                    else if (length.Max.HasValue)
                    {
                        constraints.Add($"maxlength({length.Max})");
                    }
                }
            }

            if ((parameter.TypeDescriptor.Type == typeof(int) ||
                 parameter.TypeDescriptor.Type == typeof(long)) &&
                parameter.HasAttribute <RangeAttribute>())
            {
                var range = parameter.GetAttribute <RangeAttribute>();
                if (range.Min.HasValue && range.Max.HasValue)
                {
                    constraints.Add($"range({range.Min},{range.Max})");
                }
                else if (range.Min.HasValue)
                {
                    constraints.Add($"min({range.Min})");
                }
                else if (range.Max.HasValue)
                {
                    constraints.Add($"max({range.Max})");
                }
            }

            if (parameter.HasAttribute <RegexAttribute>())
            {
                var regex = parameter.GetAttribute <RegexAttribute>()?.Regex;
                if (regex.IsNotNullOrEmpty())
                {
                    constraints.Add($"regex({regex})");
                }
            }

            if (parameter.HasAttribute <MatchTypeAttribute>() ||
                _configuration.AutomaticallyConstrainUrlParameterByType)
            {
                if (parameter.TypeDescriptor.Type == typeof(bool))
                {
                    constraints.Add("bool");
                }
                else if (parameter.TypeDescriptor.Type == typeof(DateTime))
                {
                    constraints.Add("datetime");
                }
                else if (parameter.TypeDescriptor.Type == typeof(decimal))
                {
                    constraints.Add("decimal");
                }
                else if (parameter.TypeDescriptor.Type == typeof(double))
                {
                    constraints.Add("double");
                }
                else if (parameter.TypeDescriptor.Type == typeof(float))
                {
                    constraints.Add("float");
                }
                else if (parameter.TypeDescriptor.Type == typeof(Guid))
                {
                    constraints.Add("guid");
                }
                else if (parameter.TypeDescriptor.Type == typeof(int))
                {
                    constraints.Add("int");
                }
                else if (parameter.TypeDescriptor.Type == typeof(long))
                {
                    constraints.Add("long");
                }
            }

            return(constraints);
        }
예제 #3
0
 public UrlSegment(UrlParameter parameter, List <string> constraints)
 {
     Parameter   = parameter;
     Constraints = constraints;
     IsParameter = true;
 }