PosInformatique.Foundations.Text.Templating.Razor 1.0.0

Prefix Reserved
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package PosInformatique.Foundations.Text.Templating.Razor --version 1.0.0
                    
NuGet\Install-Package PosInformatique.Foundations.Text.Templating.Razor -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PosInformatique.Foundations.Text.Templating.Razor" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PosInformatique.Foundations.Text.Templating.Razor" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="PosInformatique.Foundations.Text.Templating.Razor" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PosInformatique.Foundations.Text.Templating.Razor --version 1.0.0
                    
#r "nuget: PosInformatique.Foundations.Text.Templating.Razor, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PosInformatique.Foundations.Text.Templating.Razor@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PosInformatique.Foundations.Text.Templating.Razor&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=PosInformatique.Foundations.Text.Templating.Razor&version=1.0.0
                    
Install as a Cake Tool

PosInformatique.Foundations.Text.Templating.Razor

NuGet version NuGet downloads

Introduction

This package provides a simple way to generate text from Razor components (views) outside of ASP.NET Core MVC/Blazor pages. It is an implementation of the core PosInformatique.Foundations.Text.Templating library.

You define a Razor component with a Model parameter, and the library renders it to a TextWriter by using a RazorTextTemplate<TModel> implementation. The Razor component can also inject any service registered in the application IServiceCollection.

Install

You can install the package from NuGet:

dotnet add package PosInformatique.Foundations.Text.Templating.Razor

Features

  • Render text from Razor components (Blazor-style components)
  • Strongly-typed model passed via a Model parameter
  • Integrates with dependency injection (IServiceCollection / IServiceProvider)
  • Ability to inject any registered service directly in the Razor component with @inject
  • Simple registration via AddRazorTextTemplating(IServiceCollection)

Basic usage

1. Register the Razor text templating

You must register the Razor text templating rendering infrastructure in your DI container:

var services = new ServiceCollection();

// Register application services
services.AddLogging();

// Register Razor text templating
services.AddRazorTextTemplating();

// Build the service provider used as context
var serviceProvider = services.BuildServiceProvider();

2. Create a Razor component (view)

Create a Razor component that will be used as a template, for example HelloTemplate.razor:

@using System
@using Microsoft.Extensions.Logging

@inherits ComponentBase

@code {
    // The model automatically injected by RazorTextTemplate<TModel>
    [Parameter]
    public MyEmailModel? Model { get; set; }

    protected override void OnInitialized()
    {
        // You can use Blazor event as usual (OnInitialized, OnParametersSet, etc.)
        this.Model.Now = DateTime.UtcNow;
    }
}

Hello this.Model.UserName !
Today is this.Model.Now:U

Key points:

  • The model is received via a Model parameter (it is automatically set by the library).
  • You can inject any service registered in IServiceCollection using @inject (or [Inject] attribute in code-behind).

3. Use RazorTextTemplate<TModel>.RenderAsync()

You can now create a RazorTextTemplate<TModel> instance, build a rendering context that exposes an IServiceProvider, and call RenderAsync():

using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using PosInformatique.Foundations.Text.Templating;
using PosInformatique.Foundations.Text.Templating.Razor;

// Example of a simple ITextTemplateRenderContext implementation
public class TextTemplateRenderContext : ITextTemplateRenderContext
{
    public TextTemplateRenderContext(IServiceProvider serviceProvider)
    {
        this.ServiceProvider = serviceProvider;
    }

    public IServiceProvider ServiceProvider { get; }
}

public static class RazorTemplateSample
{
    public static async Task GenerateAsync()
    {
        var services = new ServiceCollection();
        services.AddRazorTextTemplating();

        var serviceProvider = services.BuildServiceProvider();

        // Create the Razor text template that uses the HelloTemplate component
        var template = new RazorTextTemplate<MyEmailModel>(typeof(HelloTemplate));

        // Build the context that provides IServiceProvider
        var context = new TextTemplateRenderContext(serviceProvider);

        using var writer = new StringWriter();

        // Render the template with a string model
        var model = new MyEmailModel { UserName = "John" };
        await template.RenderAsync(model, writer, context, CancellationToken.None);

        var result = writer.ToString();
        Console.WriteLine(result);
    }
}

4. Injecting other services in the Razor view

Any service registered in your IServiceCollection and available through IServiceProvider can be injected in the Razor component.

Example:

@using MyApp.Services
@inherits ComponentBase

@code {
    [Parameter]
    public MyEmailModel? Model { get; set; }

    [Inject]
    public IDateTimeProvider DateTimeProvider { get; set; } = default!

    [Inject]
    public IMyFormatter Formatter { get; set; } = default!
}

Hello @Model?.Name,

Current time: @this.DateTimeProvider.UtcNow
Formatted data: @this.Formatter.Format(Model)

As long as IDateTimeProvider and IMyFormatter are registered in the IServiceCollection, they are available during template rendering.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on PosInformatique.Foundations.Text.Templating.Razor:

Package Downloads
PosInformatique.Foundations.Emailing.Templates.Razor

Provides helpers to create EmailTemplate instances using Razor components for subject and HTML body. Built on top of PosInformatique.Foundations.Text.Templating.Razor, it supports strongly-typed models and Razor layout features for reusable email designs.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0-rc.2 50 1/26/2026
1.1.0-rc.1 70 1/23/2026
1.0.0 489 11/19/2025
1.0.0-rc.4 375 11/19/2025
1.0.0-rc.3 381 11/18/2025
1.0.0-rc.2 372 11/18/2025
1.0.0-rc.1 373 11/18/2025

1.0.0
 - Initial release with the Razor Text Templating feature.