Introduction

Welcome to this lesson on handling different HTTP methods in ASP.NET Core Razor Pages. In the previous lesson, we introduced the concept of Page Handlers and learned about the importance and role of GET and POST handlers. In this lesson, we will dive deeper into how to handle these HTTP methods effectively in Razor Pages.

Our objectives for today are:

  • To understand the difference between GET and POST methods.
  • To learn how Razor Pages handle these methods by default.
  • To implement GET and POST handlers in Razor Pages with examples.
  • To combine GET and POST methods in a single Razor Page to manage data effectively.

Understanding how to handle different HTTP methods is crucial in web development as it helps in performing a variety of operations such as displaying data, submitting forms, and updating resources.

Understanding HTTP Methods in Razor Pages

Before we dive into examples, let's briefly review what GET and POST methods are.

GET Method:

  • Used to request data from a specified resource.
  • The data sent through GET is visible in the URL.
  • It's safe and idempotent, meaning that calling it multiple times has no side effects.
  • Commonly used to retrieve data, load webpages, etc.

POST Method:

  • Used to submit data to be processed to a specified resource.
  • Data sent through POST is included in the body of the request, not the URL.
  • Not idempotent; calling it multiple times can have different effects.
  • Commonly used to submit forms, upload files, etc.

In Razor Pages, the handlers for these methods are defined within the PageModel class. By default, Razor Pages automatically map HTTP methods to handler methods based on their naming. For example, OnGet handles GET requests, and OnPost handles POST requests.

Setting Up the Main Page

Let's start by setting up our main page that will manage three items and allow editing them. We'll set up a Razor Page to display item details.

Here's the code for App/Pages/Index.cshtml:

And the code for App/Pages/Index.cshtml.cs:

This code sets up the main page that includes three links, each leading to a page for editing the corresponding item.

Implementing POST Requests: Client Code

Now, let's implement a Razor Page to handle POST requests for submitting form data. We'll use the Edit page for this. We will have the same page for editing every item, specifying the item's ID to differentiate the items.

Here's the code for App/Pages/Edit.cshtml:

Here is what happens here:

  • The @page "{id:int}" directive specifies that the page expects an id parameter of type integer - the item's ID.
  • The form uses the POST method to send the data to the server.
  • The asp-for="Name" attribute binds the input to the Name property in the EditModel class.
  • The "Submit" button submits the form, sending the POST request to the server.
  • The @if (!string.IsNullOrEmpty(Model.Message)) { <p>@Model.Message</p> } section checks if the Message property is not empty and displays it as a paragraph if it's present.
Implementing POST Requests: Server Code

After we set up the client code, here is the server code for App/Pages/Edit.cshtml.cs:

Here is a brief explanation:

  • The OnPost method handles POST requests and saves the submitted Name to a dictionary (NameStore), based on the specified item's ID that we received in the POST request.
  • If the form submission is successful, a success message is displayed.
  • After the POST request is processed, the page is reloaded to show the updated data.
Combining GET and POST in a Single Page

Next, we'll combine GET and POST methods in a single Razor Page to manage both reading and updating data seamlessly.

Here's how the combined code in Edit.cshtml.cs looks:

  • The OnGet method loads the data for the specified id when the page is requested via a GET method.
    • For example, initially, visiting /Edit/1 with a GET request will call OnGet(1) and display the data.
  • The OnPost method saves the updated data for the specified id when the form is submitted.
    • For example, submitting the form will send a POST request to the same URL, calling OnPost(1) for the first item and updating the data store.
Lesson Summary

In this lesson, we covered the basics of handling HTTP methods in Razor Pages. To recap:

  • We discussed GET and POST methods and their roles in web applications.
  • We explored how to handle GET requests to display data.
  • We learned how to handle POST requests to submit form data.
  • We combined GET and POST methods on a single page to manage data more effectively.

Remember, handling HTTP methods correctly ensures your web application can interact with users in a meaningful way. Practice writing and testing GET and POST handlers using the exercises provided.

Up next, we will be diving into more advanced aspects of Razor Pages, so stay tuned!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal