liveBook · Manning

Chapter 17: Rendering HTML using Razor views

What you will learn in chapter 17 of ASP.NET Core in Action, 3e

This chapter introduces Razor views as the HTML-generating part of Razor Pages, covering dynamic Razor markup and reusable view composition.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces Razor views as the HTML-generating part of Razor Pages, covering dynamic Razor markup and reusable view composition.

Key terms: Razor Pages, Razor Page, PageModel, page handler, Razor view, Razor template, Razor markup syntax, Razor, Razor templates, HTML

17.1 Views: Rendering the user interface

This section introduces Razor views as the presentation component that uses PageModel data and mixed C#/HTML templates to render dynamic HTML responses in Razor Pages.

Key terms: Razor view, Razor Page, page handler, PageModel, PageResult, middleware pipeline, separation of concerns, application model, view, Razor template

17.2 Creating Razor views

This section introduces Razor views as the proper way for Razor Pages and MVC controllers to generate HTML responses while keeping UI rendering separate from request-handling logic.

Key terms: Razor views, Razor Pages, MVC controllers, HTML response, page handlers, MVC separation of concerns, dynamic HTML

17.2.1 Razor views and code-behind

This section explains how a Razor Page view, its optional code-behind PageModel, and Razor directives work together to handle a request and render HTML.

Key terms: Razor view, .cshtml, code-behind, .cshtml.cs, PageModel, page handlers, @page directive, @model directive, Razor Page, [BindProperty]

17.2.2 Introducing Razor templates

This subsection introduces Razor templates as interleaved HTML and C# that render into ordinary HTML sent to the browser.

Key terms: Razor view templates, HTML markup, C# code, Razor engine, rendered, HTML output, for loop, Razor template, <li> element, to-do items

17.2.3 Passing data to views

This section explains how Razor Pages pass data from page handlers to views, emphasizing PageModel properties and reserving ViewData for values shared with layouts.

Key terms: PageModel properties, ViewData, TempData, HttpContext, @inject, dependency injection, PageModel, @model, Model, public properties

17.3 Creating dynamic web pages with Razor

This section introduces Razor’s ability to use C# while emphasizing that Razor templates should remain simple and maintainable.

Key terms: Razor syntax, .cshtml files, C#, raw HTML, Razor templates, maintain

17.3.1 Using C# in Razor templates

This section shows how Razor templates evaluate C# expressions for HTML output and run non-output C# code in code blocks.

Key terms: Razor expressions, @, @(), HTML output, PageModel, ViewData, @@, Razor expression, Razor code block, @{}

17.3.2 Adding loops and conditionals to Razor templates

This section shows how Razor templates use familiar C# loops and conditionals to generate HTML dynamically while keeping view logic limited.

Key terms: Razor templates, @ symbol, if, foreach, for, while, dynamic HTML, PageModel, IsComplete, Tasks

17.3.3 Rendering HTML with Raw

This section explains Razor’s default HTML encoding, why it protects against injection attacks, and the guarded use of Html.Raw() to render trusted HTML strings.

Key terms: Razor templates, HTML-encode, C# expressions, output stream, Html.Raw(), Html property, HTML-encoded, user input, malicious code, HtmlHelper

17.4 Layouts, partial views, and _ViewStart

This section introduces ways Razor views centralize shared markup and common view setup to reduce duplication and simplify site-wide HTML changes.

Key terms: layout, Razor views, Razor template, layouts, common markup, duplication

17.4.1 Using layouts for shared markup

This section explains how Razor layouts centralize shared page markup and insert each view's content into a common HTML structure.

Key terms: layout files, Razor templates, Pages/Shared, _Layout.cshtml, Layout property, @RenderBody(), child views, @page directive, ViewData, <title>

17.4.2 Overriding parent layouts using sections

This section explains how Razor sections place child-view content into specific locations in layouts, including nested layouts and rules for required content.

Key terms: sections, @section, @RenderSection(), child view, parent layout, required sections, optional sections, Scripts, nested layouts, Layout

17.4.3 Using partial views to encapsulate markup

This section explains how Razor partial views split reusable HTML into focused templates, how they are rendered and located, and when a view component is more appropriate.

Key terms: partial views, Razor Pages, PageModel, handlers, <partial /> Tag Helper, model, _ToDo.cshtml, partial view discovery, Pages/Shared, Views/Shared

17.4.4 Running code on every view with _ViewStart and _ViewImports

This section shows how _ViewImports and _ViewStart remove repeated Razor view setup, and how partial views can support AJAX updates with trade-offs.

Key terms: _ViewImports.cshtml, Razor directives, @using, @model, @addTagHelper, _ViewStart.cshtml, Layout, Razor Page views, layouts, partial views

17.5 Summary

This summary consolidates how Razor views generate dynamic HTML, share data and reusable markup, and safely organize view-wide behavior.

Where this is used later

Concepts explained in this chapter

← Chapter 16: Binding and validating requests with Razor Pages Chapter 18: Building forms with Tag Helpers →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.