Skip to main content

RazorSlices by Damiam Edwards

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: RazorSlices

Package Description

Author: Damiam Edwards

NuGet: https://www.nuget.org/packages/RazorSlices/

You can find more details at https://github.com/DamianEdwards/RazorSlices

Source : https://github.com/DamianEdwards/RazorSlices

Original Readme

note

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

  • ASP.NET Core 8.0 and above

  • Strongly-typed models (via @inherits RazorSlice<MyModel> or @inherits RazorSliceHttpResult<MyModel>)

  • Razor constructs:

  • DI-activated properties via @inject

  • Rendering slices from slices (aka partials) via @(await RenderPartialAsync<MyPartial>())

  • Using slices as layouts for other slices, including layouts with strongly-typed models:

    • For the layout slice, inherit from RazorLayoutSlice or RazorLayoutSlice<TModel> and use @await RenderBodyAsync() in the layout to render the body

      @inherits RazorLayoutSlice<LayoutModel>

      <!DOCTYPE html>
      <html lang="en">
      <head>
      <title>@Model.Title</title>
      @await RenderSectionAsync("head")
      </head>
      <body>
      @await RenderBodyAsync()

      <footer>
      @await RenderSectionAsync("footer")
      </footer>
      </body>
      </html>
    • For the slice using the layout, implement IUsesLayout<TLayout> or IUsesLayout<TLayout, TModel> to declare which layout to use. If using a layout with a model, ensure you implement the LayoutModel property in your @functions block, e.g

      @inherits RazorSlice<SomeModel>
      @implements IUsesLayout<LayoutSlice, LayoutModel>

      <div>
      @* Content here *@
      </div>

      @functions {
      public LayoutModel LayoutModel => new() { Title = "My Layout" };
      }
    • Layouts can render sections via @await RenderSectionAsync("SectionName") and slices can render content into sections by overriding ExecuteSectionAsync, e.g.:

      protected override Task ExecuteSectionAsync(string name)
      {
      if (name == "lorem-header")
      {
      <p class="text-info">This page renders a custom <code>IHtmlContent</code> type that contains lorem ipsum content.</p>
      }

      return Task.CompletedTask;
      }

      Note: The @section directive is not supported as it's incompatible with the rendering approach of Razor Slices

  • Asynchronous rendering, i.e. the template can contain await statements, e.g. @await WriteTheThing()

  • Writing UTF8 byte[] values directly to the output

  • Rendering directly to PipeWriter, Stream, TextWriter, StringBuilder, and string outputs, including optimizations for not boxing struct values, zero-allocation rendering of primitives like numbers, etc. (rather than just calling ToString() on everything)

  • Return a slice instance directly as an IResult in minimal APIs via @inherits RazorSliceHttpResult and Results.Extensions.RazorSlice("/Slices/Hello.cshtml")

  • Full support for trimming and native AOT when used in conjunction with ASP.NET Core Minimal APIs

Interested in supporting but not sure yet

  • Extensions to help support using HTMX with Razor Slices
  • Getting small updates to the Razor compiler itself to get some usability and performance improvements, e.g.:
    • Don't mark the template's ExecuteAsync method as an async method unless the template contains await statements to save on the async state machine overhead
    • Support compiling static template elements to UTF8 string literals (ReadOnlySpan<byte>) instead of string literals to save on the UTF16 to UTF8 conversion during rendering
    • Support disabling the default registered @addtaghelper and @model directives which rely on MVC

No intention to support

  • Tag Helpers and View Components (they're tied to MVC and are intrinsically "heavy")
  • @model directive (the Razor compiler does not support its use in conjunction with custom base-types via @inherits)
  • @attribute [Authorize] (wrong layer of abstraction for minimal APIs, etc.)
  • @section directive (the Razor compiler emits code that is incompatible with the rendering approach of Razor Slices)

About

note

Generating html from razor templates. Attention, generates IHttpResult, not html string.

How to use

Example ( source csproj, source files )

This is the CSharp Project that references RazorSlices

<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.10" />
<PackageReference Include="RazorSlices" Version="0.8.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

#pragma checksum "D:\gth\RSCG_Examples\v2\rscg_examples\RazorSlices\src\RazorDemoSlices\Slices\PersonHTML.cshtml" "{8829d00f-11b8-4213-878b-770e8597ac16}" "6b3710e80836b438a5d8935ea469d238fc095e46298456ca847e09519393bb5e"
// <auto-generated/>
#pragma warning disable 1591
[assembly: global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute(typeof(AspNetCoreGeneratedDocument.Slices_PersonHTML), @"mvc.1.0.view", @"/Slices/PersonHTML.cshtml")]
namespace AspNetCoreGeneratedDocument
{
#line default
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Mvc;
using global::Microsoft.AspNetCore.Mvc.Rendering;
using global::Microsoft.AspNetCore.Mvc.ViewFeatures;
#nullable restore
#line (3,2)-(4,1) "D:\gth\RSCG_Examples\v2\rscg_examples\RazorSlices\src\RazorDemoSlices\Slices\_ViewImports.cshtml"
using System.Globalization;

#nullable disable
#nullable restore
#line (4,2)-(5,1) "D:\gth\RSCG_Examples\v2\rscg_examples\RazorSlices\src\RazorDemoSlices\Slices\_ViewImports.cshtml"
using Microsoft.AspNetCore.Razor;

#nullable disable
#nullable restore
#line (5,2)-(6,1) "D:\gth\RSCG_Examples\v2\rscg_examples\RazorSlices\src\RazorDemoSlices\Slices\_ViewImports.cshtml"
using Microsoft.AspNetCore.Http.HttpResults;

#line default
#line hidden
#nullable disable
[global::Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemMetadataAttribute("Identifier", "/Slices/PersonHTML.cshtml")]
[global::System.Runtime.CompilerServices.CreateNewOnMetadataUpdateAttribute]
#nullable restore
internal sealed class Slices_PersonHTML : RazorSliceHttpResult<RazorDemoSlices.Person>
#nullable disable
{
#pragma warning disable 1998
public async override global::System.Threading.Tasks.Task ExecuteAsync()
{
WriteLiteral("<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n <meta charset=\"utf-8\">\r\n <title>Hello from Razor Slices!</title>\r\n</head>\r\n<body>\r\n <p>\r\n My name is ");
Write(
#nullable restore
#line (10,21)-(10,36) "D:\gth\RSCG_Examples\v2\rscg_examples\RazorSlices\src\RazorDemoSlices\Slices\PersonHTML.cshtml"
Model.FirstName

#line default
#line hidden
#nullable disable
);
WriteLiteral(" ");
Write(
#nullable restore
#line (10,38)-(10,52) "D:\gth\RSCG_Examples\v2\rscg_examples\RazorSlices\src\RazorDemoSlices\Slices\PersonHTML.cshtml"
Model.LastName

#line default
#line hidden
#nullable disable
);
WriteLiteral("\r\n </p>\r\n</body>\r\n</html>");
}
#pragma warning restore 1998
#nullable restore
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; } = default!;
#nullable disable
#nullable restore
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; } = default!;
#nullable disable
#nullable restore
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; } = default!;
#nullable disable
#nullable restore
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; } = default!;
#nullable disable
#nullable restore
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<dynamic> Html { get; private set; } = default!;
#nullable disable
}
}
#pragma warning restore 1591

Usefull

Download Example (.NET C# )

Share RazorSlices

https://ignatandrei.github.io/RSCG_Examples/v2/docs/RazorSlices

In the same category (Templating) - 10 other generators

Gobie

InterceptorTemplate

JKToolKit.TemplatePropertyGenerator

Microsoft.NET.Sdk.Razor.SourceGenerators

Minerals.AutoMixins

MorrisMoxy

RazorBlade

RSCG_IFormattable

RSCG_Templating

spreadcheetah