private static ParameterSourceAttribute GetDefaultParameterSource(ParameterInfo parameter) { if ((typeof(Guid) == parameter.ParameterType) || (typeof(DateTime) == parameter.ParameterType) || ((parameter.ParameterType.IsIdentity()) && (PopularIdentifierPropertyNames.Contains(parameter.Name, StringComparer.OrdinalIgnoreCase)))) { return(FromUrlAttribute.For(parameter)); } if ((!parameter.ParameterType.IsValueType) && (typeof(string) != parameter.ParameterType) && (!((System.TypeExtensions.IsEnumerable(parameter.ParameterType)) && (parameter.ParameterType.GetItemType().IsNumber())))) { return(FromBodyAttribute.For(parameter)); } return(FromQueryStringAttribute.For(parameter)); }
private static OperationInfo <Verb> CreateOperation(MethodInfo method, Verb verb) { string queryString = String.Empty; string uriTemplate = null; IList <ArgumentInfo> arguments = new List <ArgumentInfo>(); foreach (var parameter in method.GetParameters()) { if (parameter.IsOut) { continue; } string parameterTemplate = null; ParameterSourceAttribute source = null; if (parameter.ParameterType == typeof(Guid)) { source = FromUrlAttribute.For(parameter); uriTemplate += (parameterTemplate = "/" + parameter.Name + "/{?value}"); } else if (parameter.ParameterType.IsValueType) { source = FromQueryStringAttribute.For(parameter); queryString += (parameterTemplate = "&" + parameter.Name + "={?value}"); } else if (!parameter.ParameterType.IsValueType) { source = FromBodyAttribute.For(parameter); } arguments.Add(new ArgumentInfo(parameter, source, parameterTemplate, (parameterTemplate != null ? parameter.Name : null))); } if (queryString.Length > 0) { uriTemplate += "?" + queryString.Substring(1); } return(new OperationInfo <Verb>(method, (HttpUrl)UrlParser.Parse("/"), uriTemplate, new Regex(".*"), verb, arguments.ToArray()).WithSecurityDetailsFrom(method)); }
private static ParameterSourceAttribute GetParameterTarget(this ParameterInfo parameter) { var explicitSetting = parameter.GetCustomAttribute <ParameterSourceAttribute>(true); if (explicitSetting != null) { return(explicitSetting); } if ((typeof(Guid) == parameter.ParameterType) || (typeof(DateTime) == parameter.ParameterType) || ((IsIdentity(parameter.ParameterType)) && (PopularIdentifierPropertyNames.Contains(parameter.Name, StringComparer.OrdinalIgnoreCase)))) { return(FromUrlAttribute.For(parameter)); } if ((!parameter.ParameterType.IsValueType) && (typeof(string) != parameter.ParameterType) && (!((System.TypeExtensions.IsEnumerable(parameter.ParameterType)) && (IsNumber(parameter.ParameterType.GetItemType()))))) { return(FromBodyAttribute.For(parameter)); } return(FromQueryStringAttribute.For(parameter)); }
public void Setup() { Mock <IDefaultValueRelationSelector> defaultSourceSelector = new Mock <IDefaultValueRelationSelector>(MockBehavior.Strict); defaultSourceSelector.Setup(instance => instance.ProvideDefault(It.IsAny <ParameterInfo>(), It.IsAny <Verb>())) .Returns <ParameterInfo, Verb>((parameter, verb) => (parameter.ParameterType == typeof(int) ? (ParameterSourceAttribute)FromUrlAttribute.For(parameter) : FromBodyAttribute.For(parameter))); defaultSourceSelector.Setup(instance => instance.ProvideDefault(It.IsAny <ParameterInfo>())) .Returns <ParameterInfo>(parameter => new ToBodyAttribute()); _builder = new ControllerDescriptionBuilder <CrudController>(defaultSourceSelector.Object); }