Home

Awesome

Razor Slices

CI (main) Nuget

Lightweight Razor-based templates for ASP.NET Core without MVC, Razor Pages, or Blazor, optimized for high-performance, unbuffered rendering with low allocations. Compatible with trimming and native AOT. Great for returning dynamically rendered HTML from Minimal APIs, middleware, etc. Supports .NET 8+

Getting Started

  1. Install the NuGet package into your ASP.NET Core project (.NET 8+):

    > dotnet add package RazorSlices
    
  2. Create a directory in your project called Slices and add a _ViewImports.cshtml file to it with the following content:

    @inherits RazorSliceHttpResult
    
    @using System.Globalization;
    @using Microsoft.AspNetCore.Razor;
    @using Microsoft.AspNetCore.Http.HttpResults;
    
    @tagHelperPrefix __disable_tagHelpers__:
    @removeTagHelper *, Microsoft.AspNetCore.Mvc.Razor
    
  3. In the same directory, add a Hello.cshtml file with the following content:

    @inherits RazorSliceHttpResult<DateTime>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Hello from Razor Slices!</title>
    </head>
    <body>
        <p>
            Hello from Razor Slices! The time is @Model
        </p>
    </body>
    </html>
    

    Each .cshtml file will have a proxy type generated for it by the Razor Slices source generator that you can use as the generic argument to the various APIs in Razor Slices for rendering slices.

  4. Add a minimal API to return the slice in your Program.cs:

    app.MapGet("/hello", () => Results.Extensions.RazorSlice<MyApp.Slices.Hello, DateTime>(DateTime.Now));
    
  5. Optional: By default, all .cshtml files in your project are treated as Razor Slices. You can change this by setting the GenerateRazorSlice metadata to false for RazorSliceGenerate items in your project file, e.g.:

    <ItemGroup>
        <!-- Don't treat .cshtml files in Views or Pages directory as Razor Slices -->
        <RazorSliceGenerate Include="Views\**\*.cshtml;Pages\**\*.cshtml" GenerateRazorSlice="false" />
    </ItemGroup>
    

    This will prevent the Razor Slices source generator from generating proxy types for .cshtml files in the Views and Pages directories in your project.

Installation

NuGet Releases

Nuget

This package is currently available from nuget.org:

> dotnet add package RazorSlices

CI Builds

If you wish to use builds from this repo's main branch you can install them from this repo's package feed.

  1. Create a personal access token for your GitHub account with the read:packages scope with your desired expiration length:

    <img width="583" alt="image" src="https://user-images.githubusercontent.com/249088/160220117-7e79822e-a18a-445c-89ff-b3d9ca84892f.png">

  2. At the command line, navigate to your user profile directory and run the following command to add the package feed to your NuGet configuration, replacing the <GITHUB_USER_NAME> and <PERSONAL_ACCESS_TOKEN> placeholders with the relevant values:

    ~> dotnet nuget add source -n GitHub -u <GITHUB_USER_NAME> -p <PERSONAL_ACCESS_TOKEN> https://nuget.pkg.github.com/DamianEdwards/index.json
    
  3. You should now be able to add a reference to the package specifying a version from the repository packages feed

  4. See these instructions for further details about working with GitHub package feeds.

Features

The library is still new and features are being actively added.

Currently supported

Interested in supporting but not sure yet

No intention to support