Welcome to the first lesson of our course. In this lesson, we'll dive into Page Handlers and their critical role in web applications. By the end of this lesson, you'll understand how to work with GET
and POST
handlers and how to make your pages interact with each other.
Page Handlers are special methods in Razor Pages that handle HTTP requests. They allow you to respond to GET
, POST
, PUT
, DELETE
, and other HTTP methods. In simple terms, they allow you to define what happens when a user visits a page (GET
) or submits a form (POST
).
In this lesson, we'll focus on the two most common handlers:
OnGet
is used to handle HTTPGET
requests.OnPost
is used to handle HTTPPOST
requests.
To understand how GET
handlers work, let's look at the Contact
page example. We define App/Pages/Contact.cshtml.cs
like that:
In the above code, the OnGet
method is the GET
handler. When users navigate to the /Contact
page, this method is called. Although it's currently empty, you can add logic to initialize the page or fetch data. The [BindProperty]
attribute is used to bind the property Name
to the corresponding form field so that the value from the input field with the name Name
will automatically be mapped to the Name
property in the ContactModel
class when the form is submitted.
Then, we define App/Pages/Contact.cshtml
:
This file represents the HTML content of the page. We include an HTML form here that sends an HTTP POST request under the hood—the request is processed by the handler that we defined earlier.
Let's see how we can add some functionality to the OnGet
and OnPost
methods in our Contact
page. For this example, we will pre-populate the Name
property when the page loads. On top of that, we will log the Name
provided from the form to the console in the OnPost
method.
App/Pages/Contact.cshtml.cs
With this modification, when users navigate to the /Contact
page, the Name
property will be pre-set to "John Doe". After they input the name and click "Save", the specified name will be logged on the server side to the console. return Page()
returns the current page, meaning it will re-render the page with any changes that might have occurred during the handling of the request but does not trigger a full browser reload like a traditional page refresh
Navigating between pages is a common task in web applications. For our example, we have an Index
page that links to the Contact
page.
App/Pages/Index.cshtml
App/Pages/Index.cshtml.cs
Here, the Index
page has a simple link to navigate to the Contact
page. The OnGet
method in IndexModel
handles the initial load of the main page.
When a user clicks the link, they are directed to the Contact
page where the GET
handler in Contact.cshtml.cs
is invoked, and the form is presented.
In this lesson, we covered the basics of Page Handlers in Razor Pages. You learned about GET
and POST
handlers, saw examples of how to implement them in your code, and connected different pages using handlers. Understanding these handlers allows you to manage user interactions and data submissions seamlessly.
Now, it's time to put this knowledge into practice. Next, you'll tackle a series of exercises designed to reinforce what we've covered. Keep experimenting with GET
and POST
handlers and try navigating between different pages. Good luck, and enjoy the learning journey!
