I tried to experiment with model binding to make it easier to use our API. When using the API, I cannot get the model binding for the binding when the data is in the body, only when it is part of the request.
The code I have is:
public class FunkyModelBinder : IModelBinder { public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { var model = (Funky) bindingContext.Model ?? new Funky(); var hasPrefix = bindingContext.ValueProvider .ContainsPrefix(bindingContext.ModelName); var searchPrefix = (hasPrefix) ? bindingContext.ModelName + "." : ""; model.Funk = GetValue(bindingContext, searchPrefix, "Funk"); bindingContext.Model = model; return true; } private string GetValue(ModelBindingContext context, string prefix, string key) { var result = context.ValueProvider.GetValue(prefix + key); return result == null ? null : result.AttemptedValue; } }
When you look at the ValueProvider property on a bindingContext , I see only QueryStringValueProvider and RouteDataValueProvider , which, I think, means that if the data is in the body, I will not receive it. How can I do it? I would like to support sending data as json or form-encoded.
source share