CshtmlComponent - ASP.NET Core MVC and Razor Pages Component V2.0.0

Using components in ASP.NET Core MVC or Razor Pages, out of the box, is annoying to say the least (read: a real PITA). Tag Helpers do not support Razor syntax, View Components can not access nested child content. Razor Components do not support runtime compilation and do not work too well in standard MVC or Razor Page projects. CshtmlComponent, from the perspective of an MVC or Razor Pages app, combines the best features of these technologies.

Note: This document assumes that you have a good understanding of C#, Razor markup and ASP.NET Core.
Install the Nuget package.

CshtmlComponent

  • Razor Syntax
  • Nested Child Content
  • Runtime Compilation
  • MVC & Razor Pages
  • Lenient File Structure
  • Named Slots

Tag Helper

  • Razor Syntax
  • Nested Child Content
  • Runtime Compilation
  • MVC & Razor Pages
  • Lenient File Structure
  • Named Slots

View Component

  • Razor Syntax
  • Nested Child Content
  • Runtime Compilation
  • MVC & Razor Pages
  • Lenient File Structure
  • Named Slots

Razor Component

  • Razor Syntax
  • Nested Child Content
  • Runtime Compilation
  • MVC & Razor Pages
  • Lenient File Structure
  • Named Slots

Example

This is the C# source code of a CshtmlComponent with nested components, named slots, attributes and pure HTML content.

using Acmion.CshtmlComponent;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SampleRazorPagesApplication
{
// The associated tag of the component.
[HtmlTargetElement("ExampleComponent")]
public class ExampleComponent : CshtmlComponentBase
{
// Explicitly named attribute.
[HtmlAttributeName("Title")]
public string Title { get; set; } = "";

// These properties will default to their kebab-cased variants.
public string FontSize { get; set; } = "1rem";
public string BackgroundColor { get; set; } = "rgba(255, 0, 0, 0.1)";

// A not HTML bound property, which can not be accessed as a attribute in the component tag.
[HtmlAttributeNotBound]
public string UppercaseTitle { get; set; } = "";

public ExampleComponent(IHtmlHelper htmlHelper) : base(htmlHelper, "/Pages/Components/Example/ExampleComponent.cshtml", "div", TagMode.StartTagAndEndTag)
{
// The constructor.
// Note: Only dependency injected arguments.

// "/Pages/Components/Example/ExampleComponent.cshtml" is the path to the associated .cshtml file.
// "div" is the output tag name.
// TagMode.StartTagAndEndTag determines the tag structure, optional parameter. Defaults to TagMode.StartTagAndEndTag.

// Properties should not be accessed here, because they will not yet be set.
}

protected override Task ProcessComponent(TagHelperContext context, TagHelperOutput output)
{
// This method is called just before the associated .cshtml file is execute.
// Properties have been initialized and can be accessed.

// The property ChildContent is a string that contains the child content.

// Use this method to edit some other properties or fields.

UppercaseTitle = Title.ToUpperInvariant();

return base.ProcessComponent(context, output);
}
}
}

This is the CSHTML source code of the corresponding basic CshtmlComponent.

@* Reference the associated component as model. **@
@using SampleRazorPagesApplication
@model ExampleComponent

@* The content of the component. **@
<div class="example-component" style="padding: 1rem; background-color: @Model.BackgroundColor; font-size: @Model.FontSize">
<h1 style="margin: 0; line-height: 100%">
ExampleComponent: @Model.UppercaseTitle
</h1>

<br />

<div class="example-component-slot0" style="background: rgba(0, 255, 0, 0.1); padding: 1rem;">
@* Render another named slot. **@
@Html.Raw(Model.NamedSlots["SlotName0"])
</div>

<br />

<div class="example-component-child-content" style="background: rgba(0, 0, 255, 0.1); padding: 1rem;">
@* Render the child content. **@
@Html.Raw(Model.ChildContent)
</div>

<br />

<div class="example-component-slot1" style="background: rgba(255, 0, 0, 0.1); padding: 1rem;">
@* Render a named slot. **@
@Html.Raw(Model.NamedSlots["SlotName1"])
</div>
</div>

This is how the component is instantiated from Razor.

<ExampleComponent Title="Some title" font-size="1.2rem" background-color="rgba(0, 0, 255, 0.1)">
<div>
Some custom HTML content.
<Box>
Supports nested components.
</Box>
</div>
<CshtmlSlot Name="SlotName0">
Some custom HTML content within a named slot. The parent component decides where
this content is placed.
</CshtmlSlot>
<CshtmlSlot Name="SlotName1">
Additional custom HTML content within a second named slot.
<Box>
Supports nested components.
</Box>
</CshtmlSlot>
</ExampleComponent>

The Code

This is the entire source code of CshtmlComponent.

using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Acmion.CshtmlComponent
{
public abstract class CshtmlComponentBase : TagHelper
{
private static string CshtmlComponentContextComponentStackKey = "CshtmlComponentContextComponentStack";

[ViewContext]
[HtmlAttributeNotBound]
public ViewContext ViewContext
{
get { return _viewContext; }
set { SetViewContext(value); }
}

[HtmlAttributeNotBound]
public string? PartialViewName { get; set; }

[HtmlAttributeNotBound]
public string? OutputTagName { get; set; }

[HtmlAttributeNotBound]
public TagMode OutputTagMode { get; set; }

[HtmlAttributeNotBound]
public string ChildContent { get; set; }

[HtmlAttributeNotBound]
public Dictionary<string, string> NamedSlots { get; set; }

[HtmlAttributeNotBound]
public CshtmlComponentBase? ParentComponent { get; set; }

private IHtmlHelper _htmlHelper;
private ViewContext _viewContext;

public CshtmlComponentBase(IHtmlHelper htmlHelper, string? partialViewName, string? outputTagName, TagMode outputTagMode = TagMode.StartTagAndEndTag)
{
// IHtmlHelper htmlHelper is dependency injected by ASP.NET Core
// string partialViewName should be provided by the class that implements CshtmlComponentBase
// string outputTagName should be provided by the class that implements CshtmlComponentBase
// TagMode outputTagMode determines the tag structure

_htmlHelper = htmlHelper;
PartialViewName = partialViewName;
OutputTagName = outputTagName;
OutputTagMode = outputTagMode;

// Will be replaced in ProcessAsync.
ChildContent = "";

// Will be populated by possible CshtmlComponentSlots.
NamedSlots = new Dictionary<string, string>();

// Will be replaced in Init.
ParentComponent = null;

// Will be replaced in SetViewContext.
_viewContext = null!;
}

private void SetViewContext(ViewContext vc)
{
// Sets the ViewContext.
// Called automatically by ASP.NET Core

_viewContext = vc;

((IViewContextAware)_htmlHelper).Contextualize(ViewContext);
}


private Stack<CshtmlComponentBase> GetParentComponentList(TagHelperContext context)
{
return (context.Items[CshtmlComponentContextComponentStackKey] as Stack<CshtmlComponentBase>)!;
}
private void SetParentComponentList(TagHelperContext context, Stack<CshtmlComponentBase> parentComponentList)
{
context.Items[CshtmlComponentContextComponentStackKey] = parentComponentList;
}

protected virtual Task ProcessComponent(TagHelperContext context, TagHelperOutput output)
{
// Classes that inherit CshtmlComponentBase can override this method to edit properties etc.

return Task.CompletedTask;
}

public override sealed void Init(TagHelperContext context)
{
// Initialize

if (!context.Items.ContainsKey(CshtmlComponentContextComponentStackKey))
{
var parentComponentList = new Stack<CshtmlComponentBase>();

ParentComponent = null;
parentComponentList.Push(this);

SetParentComponentList(context, parentComponentList);
}
else
{
var parentComponentList = GetParentComponentList(context);

ParentComponent = parentComponentList.Peek();

parentComponentList.Push(this);
}

base.Init(context);
}

public override sealed void Process(TagHelperContext context, TagHelperOutput output)
{
// Method unoverridable in classes that inherit CshtmlComponentBase.

base.Process(context, output);
}

public override sealed async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
// Process the component.
// Method unoverridable in classes that inherit CshtmlComponentBase.

ChildContent = (await output.GetChildContentAsync()).GetContent();

await ProcessComponent(context, output);

var parentComponentList = GetParentComponentList(context);
parentComponentList.Pop();

output.TagName = OutputTagName;
output.TagMode = OutputTagMode;

if (PartialViewName != null)
{
var content = await _htmlHelper.PartialAsync(PartialViewName, this);
output.Content.SetHtmlContent(content);
}
}

}
}

This is the entire source code of CshtmlComponentSlot.

using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Acmion.CshtmlComponent
{
[HtmlTargetElement("CshtmlSlot")]
public class CshtmlComponentSlot : CshtmlComponentBase
{
[HtmlAttributeName("Name")]
public string Name { get; set; }

public CshtmlComponentSlot(IHtmlHelper htmlHelper) : base(htmlHelper, null, null)
{
// Will be replaced when initialized
Name = "";
}

protected override Task ProcessComponent(TagHelperContext context, TagHelperOutput output)
{
if (ParentComponent != null)
{
ParentComponent.NamedSlots[Name] = ChildContent;
}

output.SuppressOutput();

return base.ProcessComponent(context, output);
}
}
}

Usage

To create ExampleComponent with CshtmlComponent, just do the following:

  1. Install the Nuget package and add the following to a _ViewImports.cshtml file: @addTagHelper *, Acmion.CshtmlComponent. Alternatively, copy the code from The Code and paste it into .cs files in your project.
  2. Create the following files ExampleComponent.cshtml.cs and ExampleComponent.cshtml under the Pages or Views directory, depending on your project type (and other configurations).
  3. In ExampleComponent.cs:
    1. Inherit CshtmlComponentBase.
    2. Implement the constructor so that all arguments are dependency injected arguments. In most cases, it is enough that IHtmlHelper htmlHelper is the only argument. However, in the base call, you must give the path to ExampleComponent.cshtml and what output tag name the component uses. See more about output tag names in the TagHelper docs. TagMode might have to be defined, especially if using selfclosing tags, but this is optional.
    3. Add [HtmlTargetElement("ExampleComponentTag")] to the component class, where ExampleComponentTag is the tag that the component will be associated with.
    4. Optionally, specify how the tag should be closed in HtmlTargetElement, see more about how this is achieved in the TagHelper docs.
    5. List all component attributes as C# properties. ASP.NET Core will translate the properties to their kebabcased variants, unless otherwise specified with [HtmlAttributeName("AttributeName")], where AttributeName is the name of the attribute.
    6. Create fields or properties for all other values you wish to use in ExampleComponent.cshtml.
    7. Optionally, override protected virtual Task ProcessComponent(), which is called before ExampleComponent.cshtml is executed. Here you can extract information from properties and access other fields etc. This can not be done in the constructor, since any provided properties will not have been set yet. The nested child content can be accessed in this method by accessing ChildContent. The named slots can be accessed by accessing NamedSlots
  4. In ExampleComponent.cshtml:
    1. Set the model to ExampleComponent with @model ExampleComponent. You may need to add appropriate using statements.
    2. Component properties and field are accessible with Model.PropertyName.
    3. The nested child content can be accessed with Model.ChildContent. To render the child content, you can use Html.Raw(Model.ChildContent), but note that this does not encode the HTML, which means that XSS attacks are possible if non-validated user input is used as child content.
    4. Any named slots can be accessed with Model.NamedSlots. To render the slot content, you can use Html.Raw(Model.NamedSlots["SlotName"]), but note that this does not encode the HTML, which means that XSS attacks are possible if non-validated user input is used as child content. The named lots have to be defined, in the initializing context, with <CshtmlSlot Name="SlotName">[CONTENT HERE]<CshtmlSlot>. See the example in Example.
    5. Render your markup and use .cshtml files as you wish.
  5. Add a reference to the newly created component by adding @addTagHelper *, NameOfTheProjectInWhichExampleComponentResides, where NameOfTheProjectInWhichExampleComponentResides is the name of the project in which ExampleComponent can be found, to a _ViewImports.cshtml file.
  6. Initialize ExampleComponent on a page and enjoy!

Notes:

Changelog

V2.0.0

V2.0.0 Documentation

Published: Sep 19, 2020

Breaking changes. Added support for named slots and added tag helper context and output as arguments to ProcessComponent.

V1.1.0

V1.1.0 Documentation

Published: Sep 17, 2020

Added support for TagMode, which provides some extra customizability.

V1.0.0

V1.0.0 Documentation

Published: Sep 15, 2020

The initial release.

Credits

CshtmlComponent was developed by Acmion (GitHub).

Contribute to CshtmlComponent in it's GitHub repository.

WARNING: This documentation is outdated. View current documentation here.