I have a Web Api Model Binder which looks like so:
public class KeyModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
//...
}
}
I'm trying to write a rule to make it easier to test. I've found a function that works with MVC's Model Binders here:
But when attempting to convert to use webApi I cannot figure out how to populate the value provider
public TModel BindModel<TBinder, TModel>(NameValueCollection formCollection, TBinder binder)
where TBinder : IModelBinder
{
var valueProvider = new NameValueCollectionValueProvider(formCollection, null);
var dataProvider = new DataAnnotationsModelMetadataProvider();
var modelMetadata = dataProvider.GetMetadataForType(null, typeof(TModel));
var bindingContext = new ModelBindingContext
{
ModelName = typeof(TModel).Name,
ValueProvider = valueProvider,
ModelMetadata = modelMetadata
};
binder.BindModel(null, bindingContext);
return (TModel)bindingContext.ModelMetadata.Model;
}
NameValueCollection Only Exist in MVC, How do I create a value provider for Web-Api