liveBook · Manning

TagHelperContext

As explained in Pro ASP.NET Core 7, Tenth Edition

TagHelperContext is an ASP.NET Core object that provides information and shared state for a tag helper during processing of an element.

TagHelperContext carries information about the HTML element currently being transformed by an ASP.NET Core tag helper. The Process method receives it alongside TagHelperOutput, making it the helper’s view of the input element and its shared processing state. section 25.2.1

The context exposes three useful areas:

  • AllAttributes provides a read-only dictionary of attributes applied to the element, indexed by name and position. section 25.2.1
  • Items provides a dictionary for sharing values between tag helpers applied to an element and helpers applied to its descendants. section 25.3.6
  • UniqueId supplies a unique identifier for the element being transformed. section 25.2.1

Passing a row theme to cells

A RowTagHelper stores its Theme value in context.Items["theme"]. A CellTagHelper checks that shared value and assigns a Bootstrap class to each th or td. A themed tr therefore configures its descendants without repeating the theme attribute on every cell. The view must define those th and td elements explicitly; tag helpers do not process elements generated by other tag helpers. section 25.3.6

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Defines three properties and demonstrates Items coordination, while deferring broader tag-helper output behavior.
Examples
Uses BgColor and TextColor attributes, plus coordinated row, header, and cell helpers that propagate themes through Items.
What it assumes
Assumes familiarity with C# tag-helper classes and Process, while showing how element attributes reach helper properties.
What you can do after
Apply it via ASP.NET Core tag helpers to read element data and coordinate transformations across nested elements.
How it connects
Connects tag helpers with Process, TagHelperOutput, HTML attributes, descendant elements, and Bootstrap styling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.