Skip to content

StaticComponent

Abstract Inherits: TagHelper

StaticComponent is the core abstract base class for building component-driven UIs in ASP.NET Core. It extends TagHelper and uses a Razor partial view as its template, sending itself as the view model. It supports child content and named slots for flexible UI composition.

TechGems.StaticComponents

To create a component, inherit from StaticComponent and create a matching .cshtml Razor view:

using TechGems.StaticComponents;
namespace YourAssembly.Views.Components;
public class AlertComponent : StaticComponent
{
public string Type { get; set; } = "info";
}
@using YourAssembly.Views.Components;
@model AlertComponent
<div class="alert alert-@Model.Type">
@Model.ChildContent
</div>

Creates the tag helper with a default Razor view route derived automatically from the class’s full namespace. The convention maps the namespace path to a file path, replacing the assembly name with ~ and dots with /, appending .cshtml.

For example, YourAssembly.Views.Components.AlertComponent resolves to ~/Views/Components/AlertComponent.cshtml.

Creates the tag helper with a custom Razor view route, overriding the default convention.

ParameterTypeDescription
razorViewRoutestringThe explicit path to the Razor partial view.
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext? ViewContext { protected get; set; }

The View Context injected by ASP.NET Core at runtime. Provides access to the HTTP context and the HTML helper used to render partial views. This property should not be set manually.

[HtmlAttributeNotBound]
public TagHelperContent? ChildContent { get; set; }

Holds the inner HTML content placed between the component’s opening and closing tags. Access this in your Razor template via @Model.ChildContent.

[HtmlAttributeNotBound]
public bool IsChildContentNullOrEmpty { get; }

Returns true if ChildContent is null or contains only whitespace. Useful for rendering fallback content in your Razor template.

public TagHelperContent RenderSlot(string name)

Renders the content of a named slot in the Razor template. Throws ArgumentException if the slot name does not exist.

ParameterTypeDescription
namestringThe name of the slot to render.

Returns: TagHelperContent — the HTML content captured in the slot.

public bool IsSlotContentNullOrEmpty(string slotName)

Checks whether a named slot was provided and contains content. Returns true if the slot is missing, null, or whitespace-only. Useful for slot fallback logic.

ParameterTypeDescription
slotNamestringThe name of the slot to check.

Returns: bool

[HtmlAttributeNotBound]
protected StaticComponent? ParentComponent { get; set; }

A reference to the parent StaticComponent in the component hierarchy. Set automatically during the tag helper lifecycle via Init. This is null for top-level components.

protected IHtmlHelper GetHtmlHelper()

Retrieves the IHtmlHelper from the current ViewContext. Used internally for rendering partial views. Throws ArgumentNullException if ViewContext is null.

protected async Task RenderPartialView(TagHelperOutput output)

Renders the partial view using the default Razor view route and sends this (the component instance) as the model.

RenderPartialView(string viewRoute, TagHelperOutput output)

Section titled “RenderPartialView(string viewRoute, TagHelperOutput output)”
protected async Task RenderPartialView(string viewRoute, TagHelperOutput output)

Renders a partial view at the specified route, sending this as the model.

ParameterTypeDescription
viewRoutestringPath to the Razor partial view.
outputTagHelperOutputThe tag helper output to write into.

RenderPartialView<T>(string viewRoute, TagHelperOutput output, T model)

Section titled “RenderPartialView<T>(string viewRoute, TagHelperOutput output, T model)”
protected async Task RenderPartialView<T>(string viewRoute, TagHelperOutput output, T model)

Renders a partial view at the specified route with a custom model instead of the component itself.

ParameterTypeDescription
viewRoutestringPath to the Razor partial view.
outputTagHelperOutputThe tag helper output to write into.
modelTThe model to pass to the partial view.

By default, the Razor view route is derived from the component’s full type name:

{AssemblyName}.Views.Components.AlertComponent
→ ~/Views/Components/AlertComponent.cshtml

The assembly name is replaced with ~, and dots are replaced with /. If this convention doesn’t fit your project structure, use the constructor overload that accepts a custom razorViewRoute.