StaticNode
StaticNode is a lightweight abstract base class for creating leaf-level components that do not support child content or slots. Like StaticComponent, it extends TagHelper and renders a Razor partial view with itself as the model, but omits all composition features for simplicity and clarity of intent.
Use StaticNode when your component is self-contained and does not need to wrap inner HTML or accept named slots.
Namespace
Section titled “Namespace”TechGems.StaticComponentsusing TechGems.StaticComponents;
namespace YourAssembly.Views.Components;
public class AvatarComponent : StaticNode{ public string ImageUrl { get; set; } public string Name { get; set; }}@using YourAssembly.Views.Components;@model AvatarComponent
<div class="avatar"> <img src="@Model.ImageUrl" alt="@Model.Name" /> <span>@Model.Name</span></div><!-- In a Razor Page --><avatar-component image-url="/img/photo.jpg" name="Jane" />Constructors
Section titled “Constructors”StaticNode()
Section titled “StaticNode()”Creates the tag helper with a default Razor view route derived from the class’s full namespace, following the same convention as StaticComponent.
For example, YourAssembly.Views.Components.AvatarComponent resolves to ~/Views/Components/AvatarComponent.cshtml.
StaticNode(string razorViewRoute)
Section titled “StaticNode(string razorViewRoute)”Creates the tag helper with a custom Razor view route, overriding the default convention.
| Parameter | Type | Description |
|---|---|---|
razorViewRoute | string | The explicit path to the Razor partial view. |
Public Properties
Section titled “Public Properties”ViewContext
Section titled “ViewContext”[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.
Protected Methods
Section titled “Protected Methods”GetHtmlHelper()
Section titled “GetHtmlHelper()”protected IHtmlHelper GetHtmlHelper()Retrieves the IHtmlHelper from the current ViewContext. Used internally for rendering partial views. Throws ArgumentNullException if ViewContext is null.
RenderPartialView(TagHelperOutput output)
Section titled “RenderPartialView(TagHelperOutput output)”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.
| Parameter | Type | Description |
|---|---|---|
viewRoute | string | Path to the Razor partial view. |
output | TagHelperOutput | The 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.
| Parameter | Type | Description |
|---|---|---|
viewRoute | string | Path to the Razor partial view. |
output | TagHelperOutput | The tag helper output to write into. |
model | T | The model to pass to the partial view. |
StaticNode vs StaticComponent
Section titled “StaticNode vs StaticComponent”| Feature | StaticComponent | StaticNode |
|---|---|---|
| Razor partial view | Yes | Yes |
| Self as view model | Yes | Yes |
Child content (ChildContent) | Yes | No |
Named slots (RenderSlot) | Yes | No |
| Component nesting stack | Yes | No |