InlineScriptTagHelper Class

A TagHelper that inlines JavaScript files into <script> elements to reduce HTTP requests.

public sealed class InlineScriptTagHelper : InlineTagHelper

Remarks

This TagHelper is useful for embedding small, critical JavaScript files directly in HTML to eliminate additional HTTP requests and improve initial page load performance. The script content is cached in memory and automatically invalidated when the source file changes.

Example

<inline-script src="app.js" />
@* Rendered as <script>/* content of wwwroot/app.js */</script> *@
Inheritance
InlineTagHelper InlineScriptTagHelper

Constructors

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

InlineScriptTagHelper(IWebHostEnvironment, IMemoryCache, ILogger<InlineScriptTagHelper>)

InlineScriptTagHelper.cs

Initializes a new instance of the InlineScriptTagHelper class.

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

Parameters

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

Properties

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

Gets or sets the path to the JavaScript 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 external script reference to an inline <script> element.

ProcessAsync(TagHelperContext, TagHelperOutput)

InlineScriptTagHelper.cs

Processes the tag helper by converting the external script reference to an inline <script> 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