InlineImgTagHelper Class

A TagHelper that inlines image files as Base64-encoded data URIs to reduce HTTP requests.

public sealed class InlineImgTagHelper : InlineTagHelper

Remarks

This TagHelper is useful for embedding small images directly in HTML to eliminate additional HTTP requests. The image content is cached in memory and automatically invalidated when the source file changes.

Example

<inline-img src="logo.png" alt="Company Logo" />
@* Rendered as <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..." alt="Company Logo" /> *@
Inheritance
InlineTagHelper InlineImgTagHelper

Constructors

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

InlineImgTagHelper(IWebHostEnvironment, IMemoryCache, ILogger<InlineImgTagHelper>)

InlineImgTagHelper.cs

Initializes a new instance of the InlineImgTagHelper class.

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

Parameters

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

Properties

NameDescription
SrcGets or sets the path to the image file relative to the web root.

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

public string? Src { get; set; }

Property value

string?

Methods

NameDescription
ProcessAsync(TagHelperContext, TagHelperOutput)Processes the tag helper by converting the image source to an inline Base64 data URI.

ProcessAsync(TagHelperContext, TagHelperOutput)

InlineImgTagHelper.cs

Processes the tag helper by converting the image source to an inline Base64 data URI.

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