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.

7.1       Views: rendering the user interface

 
 
 

7.2       Creating Razor views

 

7.2.1   Razor views and code-behind

 
 
 
 

7.2.2   Introducing Razor templates

 
 
 
 

7.2.3   Passing data to views

 

7.3       Creating dynamic web pages with Razor

 
 

7.3.1   Using C# in Razor templates

 

7.3.2   Adding loops and conditionals to Razor templates

 
 
 

7.3.3   Rendering HTML with Raw

 
 
 

7.4       Layouts, partial views, and _ViewStart

 
 
 

7.4.1   Using layouts for shared markup

 
 
 
 

7.4.2   Overriding parent layouts using sections

 
 
 
 

7.4.3   Using partial views to encapsulate markup

 
 

7.4.4   Running code on every view with _ViewStart and _ViewImports

 
 

7.5       Selecting a view from an MVC controller

 
 

7.6       Summary

 
 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest