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













