title: Custom Boolean Parameter Binding
body: <p>I have a WebApi method, like this one:</p>
  
  <pre><code>public string Get([FromUri] SampleInput input)
  {
      //do stuff with the input...
      return "ok";
  }
  </code></pre>
  
  <p>The input is defined like this:</p>
  
  <pre><code>public class SampleInput
  {
      // ...other fields
      public bool IsAwesome { get; set; }
  }
  </code></pre>
  
  <p>As it is, it works OK: if I pass <code>&amp;isAwesome=true</code> in the query string, the parameter is initializes with the value <code>true</code>.</p>
  
  <p>My problem is that I'd like to accept both  <code>&amp;isAwesome=true</code> and <code>&amp;isAwesome=1</code> as <code>true</code> values. Currently, the second version will result in <code>IsAwesome</code> being <code>false</code> in the input model.</p>
  
  <hr>
  
  <p>What I tried, after reading the various blog posts on the subject, was to define an <code>HttpParameterBinding</code>:</p>
  
  <pre><code>public class BooleanNumericParameterBinding : HttpParameterBinding
  {
      private static readonly HashSet&lt;string&gt; TrueValues =
          new HashSet&lt;string&gt;(new[] { "true", "1" }, StringComparer.InvariantCultureIgnoreCase);
  
      public BooleanNumericParameterBinding(HttpParameterDescriptor descriptor) : base(descriptor)
      {
      }
  
      public override Task ExecuteBindingAsync(
          ModelMetadataProvider metadataProvider, 
          HttpActionContext actionContext,
          CancellationToken cancellationToken)
      {
          var routeValues = actionContext.ControllerContext.RouteData.Values;
  
          var value = (routeValues[Descriptor.ParameterName] ?? 0).ToString();
  
          return Task.FromResult(TrueValues.Contains(value));
      }
  }
  </code></pre>
  
  <p>... and register it in <em>Global.asax.cs</em>, using:</p>
  
  <pre><code>var pb = GlobalConfiguration.Configuration.ParameterBindingRules;
  pb.Add(typeof(bool), p =&gt; new BooleanNumericParameterBinding(p));
  </code></pre>
  
  <p>and</p>
  
  <pre><code>var pb = GlobalConfiguration.Configuration.ParameterBindingRules;
  pb.Insert(0, typeof(bool), p =&gt; new BooleanNumericParameterBinding(p));
  </code></pre>
  
  <p>None of these worked. My custom <code>HttpParameterBinding</code> is not being called and I still get the value <code>1</code> translated to <code>false</code>.</p>
  
  <p>How can I configure WebAPI to accept the value <code>1</code> as <code>true</code> for Booleans?</p>
  
  <p><strong>Edit:</strong> The example I presented is intentionally simplified. I have a lot of input models in my application and they contain many boolean fields that I would like to be handled in the manner described above. If there was just this one field, I would not have resorted to such complex mechanisms.</p>