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
