32 Building custom MVC and Razor Pages components

 

This chapter covers

  • Creating custom Razor Tag Helpers
  • Using view components to create complex Razor views
  • Creating a custom DataAnnotations validation attribute
  • Replacing the DataAnnotations validation framework with an alternative

In the previous chapter you learned how to customize and extend some of the core systems in ASP.NET Core: configuration, dependency injection (DI), and your middleware pipeline. These components form the basis of all ASP.NET Core apps. In this chapter we’re focusing on Razor Pages and Model-View-Controller (MVC)/API controllers. You’ll learn how to build custom components that work with Razor views. You’ll also learn how to build components that work with the validation framework used by both Razor Pages and API controllers.

We’ll start by looking at Tag Helpers. In section 32.1 I show how to create two Tag Helpers: one that generates HTML to describe the current machine and one that lets you write if statements in Razor templates without having to use C#. These will give you the details you need to create your own custom Tag Helpers in your own apps if the need arises.

32.1 Creating a custom Razor Tag Helper

32.1.1 Printing environment information with a custom Tag Helper

32.1.2 Creating a custom Tag Helper to conditionally hide elements

32.1.3 Creating a Tag Helper to convert Markdown to HTML

32.2 View components: Adding logic to partial views

32.3 Building a custom validation attribute

32.4 Replacing the validation framework with FluentValidation

32.4.1 Comparing FluentValidation with DataAnnotations attributes

32.4.2 Adding FluentValidation to your application

Summary