liveBook · Manning

Chapter 32: Building custom MVC and Razor Pages components

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

This chapter introduces ways to extend Razor views and request validation with custom Tag Helpers, view components, custom validation attributes, and FluentValidation.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces ways to extend Razor views and request validation with custom Tag Helpers, view components, custom validation attributes, and FluentValidation.

Key terms: Tag Helpers, custom Tag Helpers, view components, partial views, Razor views, DataAnnotations, validation attribute, DI container, binding models, FluentValidation

32.1 Creating a custom Razor Tag Helper

This section introduces custom Razor Tag Helpers as a way to augment, remove, or transform HTML markup while retaining readable Razor templates and IDE support.

Key terms: Tag Helpers, custom Tag Helpers, Razor templates, HTML output, Input Tag Helper, asp-for, PageModel, IntelliSense, HTML Helpers

32.1.1 Printing environment information with a custom Tag Helper

This section builds and registers a SystemInfoTagHelper that renders selectable, safely encoded server information in Razor views, partials, and layouts.

Key terms: SystemInfoTagHelper, server farm, environment, Environment Tag Helper, TagHelper, Process(), TagHelperOutput, Content.SetHtmlContent(), TagMode, <system-info>

32.1.2 Creating a custom Tag Helper to conditionally hide elements

This section builds an attribute-based Razor Tag Helper that conditionally removes an existing HTML element and its contents without wrapping markup in a C# if statement.

Key terms: IfTagHelper, if attribute, RenderContent, Tag Helper, Process(), TagHelperOutput.TagName, SuppressOutput(), Order, int.MinValue, _ViewImports.cshtml

32.1.3 Creating a Tag Helper to convert Markdown to HTML

This section builds a custom Markdown Tag Helper that renders its child Razor content, converts it to HTML with Markdig, and explains where Tag Helpers fit compared with view components.

Key terms: Markdown Tag Helper, Markdig, GetChildContentAsync, ProcessAsync, Razor content, Process, thread starvation, Tag Helpers, view components, business-logic services

32.2 View components: Adding logic to partial views

This section explains when to use view components and how to build, invoke, and render isolated Razor UI sections that combine business logic, data access, and presentation.

Key terms: view component, partial view, business logic, rendering logic, dependency injection, ViewComponent, InvokeAsync, IViewComponentResult, vc: prefix, kebab-case

32.3 Building a custom validation attribute

This section shows how to build a custom DataAnnotations validation attribute for domain-specific rules and how to obtain validation services through ValidationContext when needed.

Key terms: DataAnnotations, ValidationAttribute, CurrencyCodeAttribute, IsValid(), ValidationResult, ValidationResult.Success, filter pipeline, model binding, ValidationContext, ObjectInstance

32.4 Replacing the validation framework with FluentValidation

This section introduces FluentValidation as a replaceable alternative to DataAnnotations validation in Razor Pages and MVC, explaining its benefits and trade-offs.

Key terms: FluentValidation, DataAnnotations, Razor Pages, MVC, binding model, validator, dependency injection, DataAnnotations attributes, strongly typed, single-responsibility principle

32.4.1 Comparing FluentValidation with DataAnnotations attributes

This section compares FluentValidation with DataAnnotations by showing how standalone fluent validators make custom, reusable, dependency-injected, and cross-property validation easier.

Key terms: FluentValidation, binding model, AbstractValidator<T>, DataAnnotations attributes, RuleFor(), NotEmpty(), Must(), fluent interface, CurrencyConverterModelValidator, extension method

32.4.2 Adding FluentValidation to your application

This section explains how to install, configure, register, and organize FluentValidation validators in an MVC or Razor Pages application.

Key terms: FluentValidation.AspNetCore, AddFluentValidationAutoValidation(), Program.cs, DI container, validators, manual registration, automatic registration, DisableDataAnnotationsValidation, DataAnnotations, FluentValidation

32.5 Summary

This summary recaps how to extend Razor rendering and model validation with custom Tag Helpers, view components, validation attributes, and FluentValidation.

Concepts explained in this chapter

← Chapter 31: Advanced configuration of ASP.NET Core Chapter 33: Calling remote APIs with IHttpClientFactory →

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.