liveBook · Manning

Post-redirect-get

As explained in ASP.NET Core in Action, 3e

POST-Redirect-GET is a web interaction pattern in which a successful form submission responds with a redirect to a URL retrieved using GET, preventing refreshes from resubmitting the POST.

Post-Redirect-Get (PRG) handles successful form submissions by returning a redirect to a new URL, which the browser then retrieves with GET. Refreshing that final page repeats the GET instead of resubmitting the POST, preventing duplicate side effects such as payments. section 16.3.2

In Razor Pages, validate the submitted model first. Redisplay the form with Page() when ModelState is invalid; after successful processing, use RedirectToPage() to show a success page. section 15.6 section 16.3.2

Checkout form

A checkout handler can return Page() when validation errors exist, save the valid submission, and return RedirectToPage("Success") afterward. The browser follows the redirect with GET, so refreshing Success does not submit the checkout again. section 16.3.2

Listing 16.7 Checking model state to view the validation result
public class CheckoutModel : PageModel                              #1
{
    [BindProperty]                                                  #2
    public UserBindingModel Input { get; set; }                     #2
 
    public IActionResult OnPost()                                   #3
    {
        if (!ModelState.IsValid)                                    #4
        {
            return Page();                                          #5
        }
 
        /* Save to the database, update user, return success */     #6
 
        return RedirectToPage("Success");
    }
}

Follow the handler’s decision path from ModelState validation through database work to RedirectToPage("Success"). section 16.3.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains the request sequence and shows a Razor Pages handler, while deferring broader redirect and HTTP mechanics.
Examples
Uses a checkout or payment submission that redirects to a Success Razor Page after valid input.
What it assumes
Assumes basic Razor Pages form handling, request binding, and ModelState validation.
What you can do after
Apply it in ASP.NET Core Razor Pages with Page() for invalid input and RedirectToPage() after successful processing.
How it connects
Connects form handling with model binding, server-side validation, Razor Pages, HTTP POST, and HTTP GET.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.