liveBook · Manning

Request message

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

An HTTP POST request is an HTTP request that submits data to a server for processing or resource modification, often causing a state-changing operation.

An HTTP POST request sends data to an application for processing. In an HTML form, method="post" tells the browser to submit the entered values with POST. Use it for operations that change data, such as adding a product to a cart; GET requests should not cause changes. section 3.2.6 section 8.2.3

The URL alone does not determine what handles a request; the HTTP method matters too.

  • In a controller, [HttpGet] can select the action that displays a blank form, while [HttpPost] selects an action at the same URL that receives its submission. Model binding converts submitted values into an action parameter such as GuestResponse. section 3.2.6 section 9.4
  • In a Razor Page, OnGet handles the display request and OnPost handles submission. A named form handler, selected with asp-page-handler="Remove", invokes OnPostRemove for a POST request. section 23.4.3 section 9.2.1
  • For a web service, the URL and HTTP method together identify an operation. A Blazor WebAssembly client can send data with HttpClient.PostAsync(url, data). section 19.0 section 37.3.2

Editing a product price

A Razor Page at an id-bearing URL displays a product using OnGetAsync. Its <form method="post"> has no action attribute, so submission returns to that page’s URL. OnPostAsync obtains the id from the route and price from the form, updates the product, saves changes, and redirects. The form includes an anti-forgery token: without that field, its POST request is rejected. section 23.4.3 section 23.4.3

Listing 23.17 The contents of the Editor.cshtml.cs file in the Pages folder
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using WebApp.Models;

namespace WebApp.Pages {
   public class EditorModel : PageModel {
       private DataContext context;
               
       public Product? Product { get; set; }
               
       public EditorModel(DataContext ctx) {
           context = ctx;
       }
               
       public async Task OnGetAsync(long id) {
           Product = await context.Products.FindAsync(id);
       }
               
       public async Task<IActionResult> OnPostAsync(long id, 
               decimal price) {
           Product? p = await context.Products.FindAsync(id);
           if (p != null) {
               p.Price = price;
           }
           await context.SaveChangesAsync();
           return RedirectToPage();
       }
   }
}

The page-model listing shows the paired OnGetAsync and OnPostAsync handlers, including the database update and redirect. section 23.4.3

After processing a form, redirecting to a confirmation URL follows Post/Redirect/Get. That matters because reloading the confirmation page will not resend the POST and accidentally repeat the operation. section 27.2

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

How deep it goes
Works through form configuration, method-specific handlers, binding, anti-forgery protection, and redirects, but not the request’s wire format.
Examples
Uses an RSVP form, shopping-cart buttons, and a product-price editor to show submitted values reaching handlers.
What it assumes
Introduces the GET-versus-POST distinction while using HTML forms and C# action methods as the working context.
What you can do after
Build it from scratch by pairing a POST form with a controller action or Razor Page handler and redirecting after processing.
How it connects
Connects POST to HTML forms, model binding, Razor Page handlers, anti-forgery tokens, and Post/Redirect/Get.

Also explained in

Compare how 14 books explain request message →

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.