title: How do I replace the behaviour of Web API model binding so that instead of Null I receive a new instance when no parameters are passed in
body: <p>We have an API with many actions which take a <code>Filter</code> object. However, when someone calls an API method and doesn't pass any parameters we end up with a null reference. To avoid having to check for this everywhere we want to alter the behaviour of the model binding so that <em>for that type</em> it returns a new instance instead of null.</p>
  
  <p>Additionally, we don't really want to write our own binder for the filter type as it is likely to change frequently.</p>
  
  <p>We found a mechanism whereby we can write a <a href="http://msdn.microsoft.com/en-us/library/system.web.http.modelbinding.modelbinderparameterbinding(v=vs.118).aspx" rel="nofollow">ModelBinderParameterBinding</a> but then I can't figure out how to add that item to the WebAPI config.</p>
  
  <p>So, have we tried the correct approach and if so how do we tell WebAPI to use our new Parameter Binding?</p>
  
  <p>For reference here's the ModelParameterBindingModel... I'm definitely not sure this is production code! Since I've no way to run or test it :)</p>
  
  <pre><code>public class QueryFilterModelBinderParameterBinding : ModelBinderParameterBinding
      {
          private readonly ValueProviderFactory[] _valueProviderFactories;
         private readonly IModelBinder _binder;
  
         public QueryFilterModelBinderParameterBinding(HttpParameterDescriptor descriptor,
             IModelBinder modelBinder,
             IEnumerable&lt;ValueProviderFactory&gt; valueProviderFactories)
             : base(descriptor, modelBinder, valueProviderFactories)
         {
             if (modelBinder == null)
             {
                 throw new ArgumentNullException("modelBinder");
             }
             if (valueProviderFactories == null)
             {
                 throw new ArgumentNullException("valueProviderFactories");
             }
  
             _binder = modelBinder;
             _valueProviderFactories = valueProviderFactories.ToArray();
         }
  
         public new IEnumerable&lt;ValueProviderFactory&gt; ValueProviderFactories
         {
             get { return _valueProviderFactories; }
         }
  
         public new IModelBinder Binder
         {
             get { return _binder; }
         }
  
         public override Task ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
         {
             var ctx = GetModelBindingContext(metadataProvider, actionContext);
  
             var haveResult = _binder.BindModel(actionContext, ctx);
             //here's where we instantiate an empty filter if we cannot bind one
             var model = haveResult ? ctx.Model : new QueryFilter();
             SetValue(actionContext, model);
  
             return Task.FromResult(model);
         }
  
         private ModelBindingContext GetModelBindingContext(ModelMetadataProvider metadataProvider, HttpActionContext actionContext)
         {
             var name = Descriptor.ParameterName;
             var type = Descriptor.ParameterType;
  
             var prefix = Descriptor.Prefix;
  
             var vp = new CompositeValueProviderFactory(_valueProviderFactories).GetValueProvider(actionContext);
  
             var ctx = new ModelBindingContext()
             {
                 ModelName = prefix ?? name,
                 FallbackToEmptyPrefix = prefix == null, // only fall back if prefix not specified
                 ModelMetadata = metadataProvider.GetMetadataForType(null, type),
                 ModelState = actionContext.ModelState,
                 ValueProvider = vp
             };
  
             return ctx;
         }
      }
  </code></pre>