Tuesday, 16 December 2025

ASP.NET Core Tutorial :: An Example of the Post-Redirect-Get (PRG) Pattern in ASP.NET Core

Leave a Comment

Form submissions are a common feature of web applications, where users upload data to the server. When a user refreshes the site after completing a form, a frequent problem occurs: the browser makes the POST request again, which could result in double submissions. Developers use the Post-Redirect-Get (PRG) pattern, a well-known design strategy that improves user experience and avoids inadvertent duplicate operations, to lessen this.

Understanding the PRG Pattern

The PRG pattern follows a simple workflow:

  1. Post: The client submits data via an HTTP POST request.

  2. Redirect: The server processes the request and issues an HTTP redirect (usually 302 Found) to a new URL.

  3. Get: The client follows the redirect and issues an HTTP GET request to retrieve the updated resource or confirmation page.

This ensures that refreshing the page only repeats the GET request, not the original POST, thereby avoiding duplicate submissions.

Benefits of PRG

  • Prevents Duplicate Form Submissions: Refreshing or navigating back does not resubmit the POST request.

  • Improves User Experience: Users see a clean confirmation or result page after submission.

  • Supports Bookmarking and Sharing: Since the final step is a GET request, the resulting URL can be bookmarked or shared.

  • Aligns with REST Principles: GET requests are idempotent, making them safe for repeated execution.

When to Use the PRG Pattern

  • Form Submissions: Use PRG for any scenario where a form submission can create or change server-side data (e.g., orders, registrations, comments).

  • Multi-Step Processes: It’s useful in multi-step forms or wizards where each step might commit data changes that should not be duplicated.

Example Implementation in ASP.NET Core

Step 1: Create the Model

public class Feedback
{
    public string Name { get; set; }
    public string Comments { get; set; }
}

Step 2: Create the Controller

public class FeedbackController : Controller
{
    [HttpGet]
    public IActionResult Submit()
    {
        return View();
    }

    [HttpPost]
    public IActionResult Submit(Feedback model)
    {
        if (ModelState.IsValid)
        {
            // Process the feedback (e.g., save to database)

            // Redirect to confirmation page using PRG
            return RedirectToAction("Confirmation");
        }
        return View(model);
    }

    [HttpGet]
    public IActionResult Confirmation()
    {
        return View();
    }
}

Step 3: Create the Views

  • Submit.cshtml: Contains the feedback form.

  • Confirmation.cshtml: Displays a success message after submission.

Workflow Explanation

  1. The user navigates to /Feedback/Submit and fills out the form.

  2. On submission, the form data is posted to the server via POST.

  3. The server processes the data and issues a redirect to /Feedback/Confirmation.

  4. The browser follows the redirect and performs a GET request to display the confirmation page.

  5. If the user refreshes the confirmation page, only the GET request is repeated, not the original POST.

Best Practices

  • Always validate input before redirecting.

  • Use TempData to pass short-lived messages (e.g., success notifications) across redirects.

  • Ensure redirects point to meaningful pages that provide feedback to the user.

  • Combine PRG with anti-forgery tokens for secure form submissions.

One reliable solution to the issue of duplicate form submissions in web applications is the Post-Redirect-Get (PRG) pattern. Using RedirectToAction or Redirect, ASP.NET Core offers simple ways to implement PRG. Developers can produce apps that are safer, more user-friendly, and compliant with web standards by following this approach.

Best ASP.NET Core 10.0 Hosting Recommendation

One of the most important things when choosing a good ASP.NET Core 8.0.11 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core, their servers are optimized for PHP web applications. The performance and the uptime of the hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. 

At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core. And the engineers do regular maintenance and monitoring works to assure its Orchard hosting are security and always up.

0 comments:

Post a Comment