title: "400 Bad Request" exception error with optional parameter using JQuery Ajax with Web API
body: <p>I read the ASP.NET documentation and I'm getting there but I get "400 Bad Request" exception error that I do not understand.</p>
  
  <p>What is it about the "optional parameter" error, that cause exception error and what's the workaround to it?  The exception error is</p>
  
  <p>--snip--
  {"Message":"The request is invalid.","MessageDetail":"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String Post(Int32)' in 'BIO.Dealer.WebApp.WebApi.Member.AmazonServerDigitalLoanJacketController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."}
  --snip--</p>
  
  <p>Source code below.</p>
  
  <pre><code>//Web API Config
  public static class WebApiConfig
  {
      public static void Register(HttpConfiguration config)
      {
          // Web API configuration and services
  
          // Web API routes
          config.MapHttpAttributeRoutes();
  
          config.Routes.MapHttpRoute(
              name: "MemberApi",
              routeTemplate: "WebApi/Member/{controller}/{id}",
              defaults: new { id = RouteParameter.Optional }
          );
      }
  }
  
  //JQuery
  var jsonRequest = { "Id": 3, "Name": "Scott Fletcher"};
  
  $.ajax({
      type: "POST",
      async: false, /*false, //This need to be synchronous to the client can wait for webserver response...*/
      url: https://localhost:44301/WebApi/Member/AmazonServerDigitalLoanJacket/Upload",
      data: JSON.stringify(jsonRequest),
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: function (parmData, parmTextStatus, parmXmlHttpRequest) {
          //alert("AjaxStatus: " + parmTextStatus + "\nReadyState - " + parmXmlHttpRequest.readyState + "\nStatus: " + parmXmlHttpRequest.status + "\nStatusText: " + parmXmlHttpRequest.statusText + "\nResponseText: " + parmXmlHttpRequest.responseText);
          jsonResponse = parmData;
      },
      error: function (parmXmlHttpRequest, parmTextStatus, parmErrorThrown) {
          //alert("AjaxStatus: " + parmTextStatus + "\nReadyState - " + parmXmlHttpRequest.readyState + "\nStatus: " + parmXmlHttpRequest.status + "\nStatusText: " + parmXmlHttpRequest.statusText + "\nResponseText: " + parmXmlHttpRequest.responseText);
      }
  });
  
  //Web API Controller
  public class Foo
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  public class AmazonServerDigitalLoanJacketController : ApiController
  {
      [ActionName("Upload")]  //Upload File(s)...
      [HttpPost]
      public string Post(Foo parmFoo)
      {
          return "object of ID " + parmFoo.Id.ToString() + " (Upload) &amp; Name: " + parmFoo.Name;
      }
  }
  </code></pre>