Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


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

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.



  • Razor Syntax
  • Nested Child Content
  • Runtime Compilation
  • MVC & Razor Pages
  • Lenient File Structure
  • Named Slots
  • Reference Capturing
  • One-Off Content [1]
  • Head Content Injection [2]
  • Body Content Injection [3]
  • Render to IHtmlContent
  • Render to String

[1] One-Off Content
Component content that is rendered only for the first instantiated component.

[2] Head Content Injection
Inject HTML content to the head tag from a component.

[3] Body Content Injection
Inject HTML content to the body tag from a component.

Installation & Usage

Sample Component Usage

With CshtmlComponent one can define and use components like this. Note that this is a simple example and does not cover all the features of CshtmlComponent. Refer to the documentation for more information.


namespace SampleRazorPagesApplication.Pages.Components.Example
    // The associated tag of the component.
    public class ExampleComponent : CshtmlComponentBase
        // Explicitly named attribute.
        public string Title { get; set; } = "";

        public ExampleComponent(IHtmlHelper htmlHelper) : base(htmlHelper)
            // The constructor. 
            // Note: Only dependency injected arguments.

            // This component resolves the name of the associated .cshtml file automatically. 
            // The path to the .cshtml file must match the namespace and class name perfectly.

            // For this component the resolving logic is the following:
            //      Namespace = SampleRazorPagesApplication.Pages.Components.Example
            //      Class Name = ExampleComponent
            //      1. Remove first part of namespace and replace dots with '/'. 
            //      2. Append a '/'.
            //      3. Append Class Name.
            //      4. Append ".cshtml".
            //      Associated .cshtml file = /Pages/Components/Example/ExampleComponent.cshtml


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

<!-- The content of the component. -->
<div class="example-component" style="border: medium solid rgba(0, 0, 0, 0.1); padding: 1rem;">
        ExampleComponent: @Model.Title

    <div class="example-component-child-content">
        <!-- Render the child content. -->

Instantiating the Component

<ExampleComponent Title="Some title">
        Some custom HTML content.
            Supports nested components.

Video Demonstration

A simple video demonstration of CshtmlComponent can be found here: Skip to 11:50 for the visual results.

Sample Project

  1. Clone this repository.
  2. Start SampleRazorPageApplication.
  3. SampleRazorPageApplication/Pages/Index.cshtml uses several components.
  4. The components are defined in SampleRazorPageApplication/Pages/Components.


CshtmlComponent - ASP.NET Core MVC and Razor Pages Component








No releases published


No packages published