Aspnetcore.docs: Post Function Requires [FromBody] Attribute

Created on 3 Jul 2018  Â·  3Comments  Â·  Source: dotnet/AspNetCore.Docs

As it is given in the tutorial, the HttpPost "Create" function will not allow you to enter new data.
Given the JSON{ "name":"walk dog", "isComplete":true }, the object that is read in by the controller is

image

Updating the function signature to public IActionResult Create([FromBody]TodoItem item) allows the request data to be read properly.

image


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P4 Source - Docs.ms

Most helpful comment

@pobarlett I think the issue is because you may have forgot to anótate the controller class with the [ApiController] attribute.

All 3 comments

@pobartlett I can't reproduce this problem. The sample download works fine.

image

@pobarlett I think the issue is because you may have forgot to anótate the controller class with the [ApiController] attribute.

@pobartlett I suspect the suggestion from @Dc9894 will fix the problem. The [FromBody] attribute is only inferred for complex types when the controller is annotated with [ApiController]. [ApiController], of course, requires ASP.NET Core 2.1 or later. For ASP.NET Core 2.0 and below, the [FromBody] attribute is required.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rick-Anderson picture Rick-Anderson  Â·  3Comments

davisnw picture davisnw  Â·  3Comments

Raghumu picture Raghumu  Â·  3Comments

sonichanxiao picture sonichanxiao  Â·  3Comments

royshouvik picture royshouvik  Â·  3Comments