17 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 PagessPageModel, 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 in your mind:

  • Razor Pages—Razor Pages generally refers to the page-based paradigm that 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 such as 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.

17.1 Views: Rendering the user interface

17.2 Creating Razor views

17.2.1 Razor views and code-behind

17.2.2 Introducing Razor templates

17.2.3 Passing data to views

17.3 Creating dynamic web pages with Razor

17.3.1 Using C# in Razor templates

17.3.2 Adding loops and conditionals to Razor templates

17.3.3 Rendering HTML with Raw

17.4 Layouts, partial views, and _ViewStart

sitemap