7 Rendering HTML using Razor views
This chapter covers
- Creating Razor views to display HTML to a user
- Using C# and the Razor markup syntax to generate HTML dynamically
- Reusing common code with layouts and partial views
It’s easy to get confused between the terms involved in Razor Pages—PageModel, page handlers, Razor views—especially as some of the terms describe concrete features, and others describe patterns and concepts. We’ve touched on all these terms in detail in previous chapters, but it’s important to get them straight int your mind:
- Razor Pages—Razor Pages generally refers to the page-based paradigm which combines routing, model binding, and HTML generation using Razor views.
- Razor Page—A single Razor Page represents a single page or “endpoint”. It typically consists of two files: a .cshtml file containing the Razor view, and a .cshtml.cs file containing the page’s PageModel.
- PageModel—The PageModel for a Razor Page is where most of the action happens. It’s where you define the binding models for a page, which extracts data from the incoming request. It’s also where you define the page’s page handlers.
- Page handler—Each Razor Page typically handles a single route, but it can handle multiple HTTP verbs like GET and POST. Each page handler typically handles a single HTTP verb.
- Razor view—Razor views (also called Razor templates) are used to generate HTML. They are typically used in the final stage of a Razor Page, to generate the HTML response to send back to the user.