MVFC.RazorRender
2.0.1
See the version list below for details.
dotnet add package MVFC.RazorRender --version 2.0.1
NuGet\Install-Package MVFC.RazorRender -Version 2.0.1
<PackageReference Include="MVFC.RazorRender" Version="2.0.1" />
<PackageVersion Include="MVFC.RazorRender" Version="2.0.1" />
<PackageReference Include="MVFC.RazorRender" />
paket add MVFC.RazorRender --version 2.0.1
#r "nuget: MVFC.RazorRender, 2.0.1"
#:package MVFC.RazorRender@2.0.1
#addin nuget:?package=MVFC.RazorRender&version=2.0.1
#tool nuget:?package=MVFC.RazorRender&version=2.0.1
MVFC.RazorRender
A library for rendering Razor/Blazor components into pure HTML, with support for hybrid cache and easy integration via .NET Dependency Injection (DI). Ideal for scenarios such as email generation, PDF reports, or exporting dynamic content server-side.
Features
- Render Razor/Blazor components to pure HTML strings
- Hybrid cache support (
HybridCache) to optimize repeated renders - Simple DI integration with a single method call
- Strongly-typed parameter model via
IRazorParameter/IRazorCacheParameter - Works for email generation, reports, dynamic content export, and more
Requirements
- .NET 9.0+
- The project containing your
.razorfiles must use the Razor SDK:
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
...
</Project>
Package
| Package | Downloads |
|---|---|
| MVFC.RazorRender |
Installation
Via NuGet Package Manager:
Install-Package MVFC.RazorRender
Via .NET CLI:
dotnet add package MVFC.RazorRender
How to Use
1. Service Registration
In your Program.cs or service configuration:
Without Cache
services.AddRazorRender();
With Hybrid Cache
services.AddRazorRenderCache(options =>
{
options.DefaultEntryOptions = new HybridCacheEntryOptions
{
Expiration = TimeSpan.FromMinutes(5)
};
});
Note:
AddRazorRenderCacheinternally callsAddRazorRender, so you do not need to register both independently.
2. Defining Models and Parameter DTOs
Create a parameter DTO by implementing IRazorParameter (no cache) or IRazorCacheParameter (with cache).
// Parameter DTO for cache-aware rendering
public sealed record CommentParameterDto(Post Model, string CacheKey) : IRazorCacheParameter;
// Domain models
public sealed record Post(int Id, string Title, string Content, IReadOnlyList<Comment> Comments);
public sealed record Comment(string Name, int Age);
// Razor view component
public partial class BlogView : ComponentBase
{
[Parameter]
public required Post Model { get; set; }
}
Important:
CacheKeyinIRazorCacheParameteris automatically excluded from theParameterViewpassed to the Razor component, so you don't need to declare it as a Blazor parameter.
3. Dependency Injection
Inject the desired service into your class:
public sealed class MyService(
IRazorHtmlRenderService razorRenderService, // Without cache
ICacheRazorHtmlRenderService cacheRazorHtmlRender) // With cache
{
private readonly IRazorHtmlRenderService _razorRenderService = razorRenderService;
private readonly ICacheRazorHtmlRenderService _cacheRazorHtmlRender = cacheRazorHtmlRender;
// ...
}
4. Rendering a Razor Component
var model = MockFactory.CreatePostMock();
var parameters = new CommentParameterDto(Model: model, CacheKey: "post-123");
// Without cache
string html = await _razorRenderService.GenerateHtmlAsync<BlogView>(parameters);
// With cache (returns cached result on subsequent calls with the same key)
string htmlCache = await _cacheRazorHtmlRender.GenerateHtmlAsync<BlogView>(parameters);
5. Automated Test Example
[Fact(DisplayName = "Razor HTML rendering of Post")]
public async Task Test_Post_RazorHtmlRender()
{
var model = MockFactory.CreatePostMock();
var parameters = new CommentParameterDto(Model: model, CacheKey: "test");
string html = await _razorRenderService.GenerateHtmlAsync<BlogView>(parameters);
Assert.NotNull(html);
}
Key Interfaces & Classes
| Type | Description |
|---|---|
IRazorHtmlRenderService |
Renders Razor components to HTML without cache |
ICacheRazorHtmlRenderService |
Renders Razor components to HTML with hybrid cache |
IRazorParameter |
Marker interface for render parameters |
IRazorCacheParameter |
Extends IRazorParameter with a CacheKey property |
BaseHtmlRenderService<T> |
Abstract base class shared by both service implementations |
RazorExtensions |
Extension methods: AddRazorRender() and AddRazorRenderCache() |
How it Works
- Your parameter DTO's public properties are automatically reflected and mapped into a
ParameterView. - If the parameter implements
IRazorCacheParameter, theCacheKeyproperty is excluded from the component parameters (it's only used as the cache lookup key). - The rendered HTML is decoded and line endings are stripped via
CleanGeneratedHtml()for a clean, single-line output.
Contributing
See CONTRIBUTING.md.
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 is compatible. 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. |
-
net10.0
- Microsoft.AspNetCore.Components.Web (>= 10.0.4)
- Microsoft.Extensions.Caching.Hybrid (>= 10.4.0)
- Microsoft.Extensions.Logging (>= 10.0.4)
-
net9.0
- Microsoft.AspNetCore.Components.Web (>= 9.0.14)
- Microsoft.Extensions.Caching.Hybrid (>= 9.10.0)
- Microsoft.Extensions.Logging (>= 9.0.14)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.