InlineStyleTagHelper Class

A TagHelper that inlines CSS files into <style> elements to reduce HTTP requests.

public sealed class InlineStyleTagHelper : InlineTagHelper

Remarks

This TagHelper is particularly useful for critical CSS optimization, where you want to inline above-the-fold styles to improve initial page render performance. The CSS content is cached in memory and automatically invalidated when the source file changes.

Example

<inline-style href="site.css" />
@* Rendered as <style>/* content of wwwroot/site.css */</style> *@
Inheritance
InlineTagHelper InlineStyleTagHelper

Constructors

NameDescription
InlineStyleTagHelper(IWebHostEnvironment, IMemoryCache, ILogger<InlineStyleTagHelper>)Initializes a new instance of the InlineStyleTagHelper class.

InlineStyleTagHelper(IWebHostEnvironment, IMemoryCache, ILogger<InlineStyleTagHelper>)

InlineStyleTagHelper.cs

Initializes a new instance of the InlineStyleTagHelper class.

public InlineStyleTagHelper(IWebHostEnvironment webHostEnvironment, IMemoryCache cache, ILogger<InlineStyleTagHelper> logger)

Parameters

webHostEnvironment IWebHostEnvironment
The web host environment for accessing web root files.
cache IMemoryCache
The memory cache for storing file contents.
logger ILogger<InlineStyleTagHelper>
The logger used to report missing files.

Properties

NameDescription
HrefGets or sets the path to the CSS file relative to the web root.

Gets or sets the path to the CSS file relative to the web root.

public string? Href { get; set; }

Property value

string?

Methods

NameDescription
ProcessAsync(TagHelperContext, TagHelperOutput)Processes the tag helper by converting the external CSS reference to an inline <style> element.

ProcessAsync(TagHelperContext, TagHelperOutput)

InlineStyleTagHelper.cs

Processes the tag helper by converting the external CSS reference to an inline <style> element.

net10.0

public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)

net11.0

public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)

Parameters

context TagHelperContext
Contains information about the current tag.
output TagHelperOutput
A stateful HTML element used to generate an HTML tag.

Returns

Task