title: Binding source parameter inference in ASP.NET core
body: <p>I'm currently upgrading a project from <a href="https://www.nuget.org/packages/Microsoft.AspNet.WebApi/5.2.6" rel="nofollow noreferrer">ASP.NET WebAPI 5.2.6</a> (OWIN) to <a href="https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.Core/2.1.1" rel="nofollow noreferrer">ASP.NET Core 2.1.1</a> (Kestrel).</p>
  
  <p>Our project is a single page application and we communicate via WebAPI with the client. Therefore I wanted to annotate the controllers wit the new <code>ApiController</code> attribute. </p>
  
  <p>Unfortunately it seems that the <a href="https://docs.microsoft.com/en-us/aspnet/core/web-api/index?view=aspnetcore-2.1#binding-source-parameter-inference" rel="nofollow noreferrer">binding source parameter inference</a> isn't working as expected (at least for me). I assumed based on the docs, that complex types (e.g. my <code>LoginRequest</code>) are inferred as <code>[FromBody]</code>.</p>
  
  <h3>Code (Controller &amp; Startup)</h3>
  
  <pre><code>// AccountController.cs
  
  [Route("/account"), ApiController]
  public class AccountController : ControllerBase
  {
      [HttpPost("backendLogin"), AllowAnonymous]
      public async Task&lt;ActionResult&lt;LoginResponse&gt;&gt; BackendLogin(LoginRequest lr)
      {
          await Task.CompletedTask.ConfigureAwait(false); // do some business logic
          return Ok(new LoginResponse {UserId = "123"});
      }
  
      // Models
  
      public class LoginRequest {
          public string Email { get; set; }
          public string Password { get; set; }
      }
  
      public class LoginResponse {
          public string UserId { get; set; }
      }
  }
  
  // Startup.cs
  
  public void ConfigureServices(IServiceCollection services) {
      services.AddMvcCore()
                      .AddJsonFormatters(settings =&gt; {
                          settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
                          settings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
                          settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                      })
                      .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  
      services.Configure&lt;ApiBehaviorOptions&gt;(options =&gt; {
          // options.SuppressConsumesConstraintForFormFileParameters = true;
          // options.SuppressInferBindingSourcesForParameters = true;
          // options.SuppressModelStateInvalidFilter = true;
      });
  }
  
  public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
      if (env.IsDevelopment()) {
          app.UseDeveloperExceptionPage();
      }
      app.UseMvc();
  }
  </code></pre>
  
  <h3>Problem</h3>
  
  <p>Calling the controller from the client via Ajax call (<code>Content-Type: application/x-www-form-urlencoded;</code>) results in a 400 (bad request) response, with content <code>{"":["The input was not valid."]}</code>. On the server I get the following trace output:
  <code>Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor: Information: Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.SerializableError'.</code></p>
  
  <p>If I change the <code>options.SuppressInferBindingSourcesForParameters</code> in <code>ConfigureServices</code> to true, it seems to work. This is strange, since this setting should disable the binding inference or have I misconceived something? Is this a bug in ASP.NET core or am I missing something?</p>
  
  <p>Btw. it also works if I ommit the <code>ApiController</code> attribute, but I guess this is not the real solution to this problem.
  Furthermore I would be happy if I don't need to change anything on the client side (adding headers, change content types, ...), because there are a lot of Ajax calls out there and I just want to upgrade the server side components.</p>