Skip to content

Latest commit

 

History

History
79 lines (57 loc) · 3.27 KB

README.md

File metadata and controls

79 lines (57 loc) · 3.27 KB

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

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

Method Mean Error StdDev Rank Gen0 Gen1 Allocated
'Render .cshtml View' 21.279 us 0.0556 us 0.0331 us 2 0.7935 - 6.95 KB
'Render .razor Component' 4.108 us 1.4003 us 0.9262 us 1 0.1907 0.0610 1.59 KB
'Render+Inline .cshtml View' 76.252 us 8.5756 us 4.4852 us 4 7.3242 0.4883 59.86 KB
'Render+Inline .razor Component' 37.275 us 5.6494 us 3.3619 us 3 6.1035 1.4648 51.13 KB