liveBook · Manning

FromBody attribute

As explained in Pro ASP.NET Core 7, Tenth Edition

The FromBody attribute is an ASP.NET Core parameter attribute that specifies that a parameter value should be read from the HTTP request body.

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

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Shows parameter placement and a working POST request, but defers the complexities of model binding and data validation.
Examples
Uses Product actions and PowerShell JSON POST and PUT requests, including a POST whose bound object is returned.
What it assumes
Assumes familiarity with controllers and action parameters while demonstrating request-body binding through a Product action.
What you can do after
Apply it via ASP.NET Core MVC to bind a JSON request body to a controller action parameter.
How it connects
Connects model binding to HTTP POST and PUT actions, JSON, data validation, and the ApiController attribute.

Also explained in

Compare how 2 books explain FromBody attribute →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.