/// <summary> /// Bind the parameter as if it had the given attribute on the declaration. /// </summary> /// <param name="parameter">parameter to provide binding for.</param> /// <param name="attribute">attribute to describe the binding.</param> /// <returns>a binding</returns> public static HttpParameterBinding BindWithAttribute( this HttpParameterDescriptor parameter, ParameterBindingAttribute attribute ) { return(attribute.GetBinding(parameter)); }
/// <summary> /// Called from the generated lambda expressions to perform the parameter binding. /// </summary> private static T SetProperty <T>(IDotvvmRequestContext context, ParameterBindingAttribute attribute, T defaultValue) { if (attribute.TryGetValue <T>(context, out var result)) { return(result); } return(defaultValue); }
/// <summary> /// <see cref="ParameterBindingAttribute"/> 获取绑定 <see cref="ParameterBinding"/> /// </summary> /// <param name="parameter"></param> /// <param name="attribute"></param> /// <returns></returns> public static ParameterBinding BindWithAttribute(this ParameterDescriptor parameter, ParameterBindingAttribute attribute) { if (parameter == null) { throw new ArgumentNullException(nameof(parameter)); } return(attribute.GetBinding(parameter)); }
static ParameterBinder GetParameterBinder(ParameterBindingAttribute a) { if (a == null) { return(ParameterBinder.None); } if (a is FromUriAttribute) { return(ParameterBinder.FromUri); } if (a is FromBodyAttribute) { return(ParameterBinder.FromBody); } throw new ArgumentException($"How can it be with this ParameterBindingAttribute {a.ToString()}", nameof(a)); }
private static ParameterBinding GetParameterBinding(ParameterDescriptor descriptor) { ParameterBindingAttribute attribute = descriptor.ParameterBindingAttribute; if (attribute != null) { return(attribute.GetBinding(descriptor)); } /* * TODO: Configure 自定义配置项绑定器 */ Type type = descriptor.ParameterType; if (TypeHelper.CanConvertFromString(type)) { return(descriptor.BindWithAttribute(new FromUriAttribute())); } return(descriptor.BindWithAttribute(new FromBodyAttribute())); }
// Determine how a single parameter will get bound. // This is all sync. We don't need to actually read the body just to determine that we'll bind to the body. protected virtual HttpParameterBinding GetParameterBinding( HttpParameterDescriptor parameter ) { // Attribute has the highest precedence // Presence of a model binder attribute overrides. ParameterBindingAttribute attr = parameter.ParameterBinderAttribute; if (attr != null) { return(attr.GetBinding(parameter)); } // No attribute, so lookup in global map. ParameterBindingRulesCollection pb = parameter.Configuration.ParameterBindingRules; if (pb != null) { HttpParameterBinding binding = pb.LookupBinding(parameter); if (binding != null) { return(binding); } } // Not explicitly specified in global map or attribute. // Use a default policy to determine it. These are catch-all policies. Type type = parameter.ParameterType; if (TypeHelper.CanConvertFromString(type)) { // For simple types, the default is to look in URI. Exactly as if the parameter had a [FromUri] attribute. return(parameter.BindWithAttribute(new FromUriAttribute())); } // Fallback. Must be a complex type. Default is to look in body. Exactly as if this type had a [FromBody] attribute. attr = new FromBodyAttribute(); return(attr.GetBinding(parameter)); }
/// <summary> /// Bind the parameter as if it had the given attribute on the declaration. /// </summary> /// <param name="parameter">parameter to provide binding for.</param> /// <param name="attribute">attribute to describe the binding.</param> /// <returns>a binding</returns> public static HttpParameterBinding BindWithAttribute(this HttpParameterDescriptor parameter, ParameterBindingAttribute attribute) { return attribute.GetBinding(parameter); }