20 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, 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 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 20.1 I’ll show you how to create two different 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.

20.1 Creating a custom Razor Tag Helper

20.1.1 Printing environment information with a custom Tag Helper

20.1.2 Creating a custom Tag Helper to conditionally hide elements

20.1.3 Creating a Tag Helper to convert Markdown to HTML

20.2 View components: Adding logic to partial views

20.3 Building a custom validation attribute

20.4 Replacing the validation framework with FluentValidation

20.4.1 Comparing FluentValidation to DataAnnotations attributes

20.4.2 Adding FluentValidation to your application

Summary