DamianEdwards / RazorSlices

Lightweight Razor-based templates for ASP.NET Core without MVC, Razor Pages, or Blazor.
MIT License
312 stars 13 forks source link

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));

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:

    image

  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