You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
// AccountController.cs
[Route("/account"), ApiController]
public class AccountController : ControllerBase {
[HttpPost("backendLogin"), AllowAnonymous]
public async Task<ActionResult<LoginResponse>> 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 => {
settings.DateTimeZoneHandling = DateTimeZoneHandling.Utc;
settings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.Configure<ApiBehaviorOptions>(options => {
// options.SuppressConsumesConstraintForFormFileParameters = true;
// options.SuppressInferBindingSourcesForParameters = true;
// options.SuppressModelStateInvalidFilter = true;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
Description of the problem:
It seems that the binding source parameter inference isn't working as expected (at least for me). I assumed based on the docs, that complex types (e.g. my LoginRequest) are inferred as [FromBody].
If I change the options.SuppressInferBindingSourcesForParameters in ConfigureServices 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?
Btw. it also works if I ommit the ApiController attribute, but I guess this is not the real solution to this problem
PS: I would also suggest to update the documentation for ApiBehaviorOptions.SuppressInferBindingSourcesForParameters. Maybe it is just me, but it was not clear on first sight what this property does based on the documentation. The second paragraph starts with When enabled, ..., I first thought it means when this property is set to true, but I think it really means when the "Binding inference" is enabled.
Version of Microsoft.AspNetCore.Mvc or Microsoft.AspNetCore.App or Microsoft.AspNetCore.All: 2.1.1
The text was updated successfully, but these errors were encountered:
ApiController is designed for REST-client specific scenarios and isn't designed towards browser based (form-urlencoded) requests. FromBody assumes JSON \ XML request bodies and it'll attempt to serialize it, which is not what you want with form url encoded content. Using a vanilla (non-ApiController) would be the way to go here.
The second paragraph starts with When enabled, ..., I first thought it means when this property is set to true, but I think it really means when the "Binding inference" is enabled.
It does mean when the property is set to true \ when the feature is enabled. We'd be happy to accept PRs to update it if you think it could be worded better.
Is this a Bug or Feature request?:
Question, I've also created a SO question.
Steps to reproduce (preferably a link to a GitHub repo with a repro project):
application/x-www-form-urlencoded
email=asdf%40asdf.com&password=asdf
Description of the problem:
It seems that the binding source parameter inference isn't working as expected (at least for me). I assumed based on the docs, that complex types (e.g. my LoginRequest) are inferred as
[FromBody]
.If I change the
options.SuppressInferBindingSourcesForParameters
inConfigureServices
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?Btw. it also works if I ommit the ApiController attribute, but I guess this is not the real solution to this problem
PS: I would also suggest to update the documentation for ApiBehaviorOptions.SuppressInferBindingSourcesForParameters. Maybe it is just me, but it was not clear on first sight what this property does based on the documentation. The second paragraph starts with
When enabled, ...
, I first thought it means when this property is set to true, but I think it really means when the "Binding inference" is enabled.Version of
Microsoft.AspNetCore.Mvc
orMicrosoft.AspNetCore.App
orMicrosoft.AspNetCore.All
: 2.1.1The text was updated successfully, but these errors were encountered: