ASP.NET Core MVC Interview Question #29
What is the Post/Redirect/Get pattern in ASP.NET Core MVC?
Controllers, Actions, Model Binding & Action Results Junior Beginner
Quick Interview Answer
Post/Redirect/Get (PRG) is a pattern where a successful POST request returns a redirect instead of directly rendering the final page. The browser then performs a GET request for the redirected URL.
Detailed Explanation
Post/Redirect/Get (PRG) is a pattern where a successful POST request returns a redirect instead of directly rendering the final page. The browser then performs a GET request for the redirected URL.
PRG reduces accidental duplicate form submissions when users refresh the page and gives the final page a normal GET URL. If validation fails, the POST action usually returns the same form view directly so validation errors remain available.
Code Example
[HttpPost]
public async Task<IActionResult> Create(ProductEditViewModel model)
{
if (!ModelState.IsValid)
return View(model);
await _service.CreateAsync(model);
return RedirectToAction(nameof(Index));
}