26 Using the built-in tag helpers

 

This chapter covers

  • Creating anchor elements that target actions and Razor Pages
  • Managing JavaScript and CSS files
  • Working with image elements
  • Caching fragments of content
  • Generating content based on the hosting environment

ASP.NET Core provides a set of built-in tag helpers that apply the most commonly required element transformations. In this chapter, I explain those tag helpers that deal with anchor, script, link, and image elements, as well as features for caching content and selecting content based on the environment. In chapter 27, I describe the tag helpers that support HTML forms. Table 26.1 puts the built-in tag helpers in context.

Table 26.1 Putting the built-in tag helpers in context (view table figure)

Question

Answer

What are they?

The built-in tag helpers perform commonly required transformations on HTML elements.

Why are they useful?

Using the built-in tag helpers means you don’t have to create custom helpers using the techniques in chapter 25.

How are they used?

The tag helpers are applied using attributes on standard HTML elements or through custom HTML elements.

Are there any pitfalls or limitations?

No, these tag helpers are well-tested and easy to use. Unless you have unusual needs, using these tag helpers is preferable to custom implementation.

Are there any alternatives?

These tag helpers are optional, and their use is not required.

Table 26.2 provides a guide to the chapter.

26.1 Preparing for this chapter

26.1.1 Adding an image file

26.1.2 Installing a client-side package

26.1.3 Dropping the database

26.1.4 Running the example application

26.2 Enabling the built-in tag helpers

26.3 Transforming anchor elements

26.3.1 Using anchor elements for Razor Pages

26.4 Using the JavaScript and CSS tag helpers

26.4.1 Managing JavaScript files

26.4.2 Managing CSS stylesheets

26.5 Working with image elements

26.6 Using the data cache

26.6.1 Setting cache expiry

26.7 Using the hosting environment tag helper

Summary