RG.RazorMail 1.9.0

dotnet add package RG.RazorMail --version 1.9.0                
NuGet\Install-Package RG.RazorMail -Version 1.9.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="RG.RazorMail" Version="1.9.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add RG.RazorMail --version 1.9.0                
#r "nuget: RG.RazorMail, 1.9.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.
// Install RG.RazorMail as a Cake Addin
#addin nuget:?package=RG.RazorMail&version=1.9.0

// Install RG.RazorMail as a Cake Tool
#tool nuget:?package=RG.RazorMail&version=1.9.0                

RG.RazorMail

NuGet .NET

Render razor view to html and inline css. This library simply wraps RazorLight and PreMailer.Net

Setup

1. For Rendering .cshtml Views

1.1. Add to service collection
services.AddRazorMail(options => {
    options.ViewAssembly = Assembly.GetEntryAssembly();
});
1.2. Set PreserveCompilationContext to true in your .csproj file
<PropertyGroup>
  <PreserveCompilationContext>true</PreserveCompilationContext>
</PropertyGroup>
1.3. Set build action of your razor views to EmbeddedResource
1.4. Use relative path of your razor views as viewName
string html = await _razorMail.RenderAsync("Views/Home/Index.cshtml", model);

2. For Rendering .razor Components

2.1. Add to service collection
services.AddRazorMail();

Available methods

// Render razor view to string
Task<string> RenderAsync<T>(string viewName, T model);

// Render razor component to string
Task<string> RenderComponentAsync<TComponent>(IDictionary<string, object>? parameters);

// Render razor view to string and inline css
Task<string> RenderAndInlineCssAsync<T>(string viewName, T model, string? css = null);

// Render razor component to string and inline css
Task<string> RenderComponentAndInlineCssAsync<TComponent>(IDictionary<string, object>? parameters, string? css = null);

// Inline css only
string InlineCss(string html, string? css = null);

Benchmark Results

Intel Core i7-10700 CPU 2.90GHz, 1 CPU, 16 logical and 8 physical cores .NET SDK 8.0.101

Method Mean Error StdDev Rank Gen0 Gen1 Allocated
'Render .cshtml View' 67.514 us 0.8551 us 0.5088 us 3 0.8545 - 7.06 KB
'Render .razor Component' 2.808 us 0.1450 us 0.0759 us 1 0.1984 0.0648 1.64 KB
'Render+Inline .cshtml View' 184.443 us 83.4985 us 55.2291 us 4 6.8359 0.4883 59.22 KB
'Render+Inline .razor Component' 43.990 us 1.3244 us 0.6927 us 2 5.8594 1.7090 48.73 KB
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.9.0 26 11/13/2024
1.0.7 2,237 2/15/2024
1.0.6 111 2/15/2024
1.0.5 128 2/15/2024
1.0.4 118 2/14/2024
1.0.3 124 2/13/2024
1.0.2 419 11/13/2023
1.0.1 844 11/30/2022
1.0.0 593 3/2/2022