title: Optional Parameters in Web Api Attribute Routing
body: <p>I want to handle POST of the following API-Call:</p>
  
  <p><code>/v1/location/deviceid/appid</code></p>
  
  <p>Additional Parameter are coming from the Post-Body.</p>
  
  <p>This all works fine for me. Now I wnat to extend my code by allowing "deviceid" and/or "appid" and/or BodyData to be null:</p>
  
  <pre><code>/v1/location/deviceid
  /v1/location/appid
  /v1/location/
  </code></pre>
  
  <p>These 3 URLs should responded by the same route.</p>
  
  <p>My first approach (BodyData required):</p>
  
  <pre><code>[Route("v1/location/{deviceid}/{appid}", Name = "AddNewLocation")]
  public location_fromuser Post(string deviceid = null, string appid = null, [FromBody] location_fromuser BodyData)
  {
      return repository.AddNewLocation(deviceid, appid, BodyData);
  }
  </code></pre>
  
  <p>This does not work and returns a compile error:</p>
  
  <blockquote>
    <p>"optional Parameters must be at the end"</p>
  </blockquote>
  
  <p>Next try:</p>
  
  <pre><code>[Route("v1/location/{deviceid}/{appid}", Name = "AddNewLocation")]
  public location_fromuser Post([FromBody] location_fromuser BodyData, string deviceid = null, string appid = null)
  </code></pre>
  
  <p>Now my function AddNewLocation() get always an <code>BodyData=null</code> - even if the call send the Body.</p>
  
  <p>Finally I set all 3 Parameter optional:</p>
  
  <pre><code>[Route("v1/location/{deviceid}/{appid}", Name = "AddNewLocation")]
  public location_fromuser Post(string deviceid = null, string appid = null, [FromBody location_fromuser BodyData = null)
  </code></pre>
  
  <p>Don´t work: </p>
  
  <blockquote>
    <p>Optional parameter <code>BodyData</code> is not supported by <code>FormatterParameterBinding</code>.</p>
  </blockquote>
  
  <p>Why do I want a solution with optional Parameters? My Controller handles just the "adding of a new Location" via a POST.</p>
  
  <p>I want to send on wrong data my own exceptions or error messages. Even if the call has missing values. In this case I want to be able to decide to throw an exception or Setting Defaults by my code.</p>