23 Using Razor Pages

 

This chapter covers

  • Generating HTML content with Razor Pages
  • Routing HTTP requests to Razor Pages
  • Using single-file Razor Pages and using a separate code-behind class file
  • Using layouts and partial views with Razor Pages

In this chapter, I introduce how to use Razor Pages, which is a simpler approach to generating HTML content, intended to capture some of the enthusiasm for the legacy ASP.NET Web Pages framework. I explain how Razor Pages work, explain how they differ from the controllers and views approach taken by the MVC Framework, and show you how they fit into the wider ASP.NET Core platform.

The process of explaining how Razor Pages work can minimize the differences from the controllers and views described in earlier chapters. You might form the impression that Razor Pages are just MVC-lite and dismiss them, which would be a shame. Razor Pages are interesting because of the developer experience and not the way they are implemented.

23.1 Preparing for this chapter

23.1.1 Running the example application

23.2 Understanding Razor Pages

23.2.1 Configuring Razor Pages

23.2.2 Creating a Razor Page

23.3 Understanding Razor Pages routing

23.3.1 Specifying a routing pattern in a Razor Page

23.3.2 Adding routes for a Razor Page

23.4 Understanding the Page model class

23.4.1 Using a code-behind class file

23.4.2 Understanding action results in Razor Pages

23.4.3 Handling multiple HTTP methods

23.4.4 Selecting a handler method

23.5 Understanding the Razor Page view

23.5.1 Creating a layout for Razor Pages

23.5.2 Using partial views in Razor Pages

23.5.3 Creating Razor Pages without page models

Summary