The ASP.NET Core MVC `[FromBody]` attribute tells model binding to obtain an action parameter from the HTTP request body. For a JSON request, MVC decodes the body, creates an object, and populates its properties before invoking the action. section 19.4.2 section 28.6.3
Receive a product
In a `ProductsController` routed to `api/products`, `[HttpPost]` selects POST requests while `SaveProduct([FromBody] Product product)` receives the submitted Product. The action adds it to the data context. A PowerShell `Invoke-RestMethod` POST to `http://localhost:5000/api/products`, with a JSON body and `-ContentType "application/json"`, exercises the action; a subsequent GET shows the stored object. section 19.4.2
Listing 28.32 Adding an action method in the FormController.cs file in the Controllers folder
using Microsoft.AspNetCore.Mvc;
using WebApp.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Text.Json;
namespace WebApp.Controllers {
[AutoValidateAntiforgeryToken]
public class FormController : Controller {
private DataContext context;
public FormController(DataContext dbContext) {
context = dbContext;
}
// ...other actions omitted for brevity...
[HttpPost]
[IgnoreAntiforgeryToken]
public Product Body([FromBody] Product model) {
return model;
}
}
}
This complete action shows the attribute on a `Product` parameter and returns the bound object so the result can be inspected. Its anti-forgery exception is there because the test request has no anti-forgery token. section 28.6.3
`[FromBody]` is useful when a client sends JSON rather than form data. It also appears on a PUT action that receives a replacement Product. Do not add it by reflex: a controller decorated with `[ApiController]` does not require it to select body data. Model binding and data validation have further behavior beyond this simple binding example. section 19.4.2 section 19.5.5

