Razor.Templating.Core is an open-source .NET library that renders Razor view files (.cshtml) to HTML strings using precompiled views from the Razor SDK, and is distributed as the Razor.Templating.Core NuGet package.
What is Razor.Templating.Core?
Razor.Templating.Core is a NuGet library that takes a .cshtml view path, an optional model object, and a dictionary of ViewData/ViewBag values, and returns the rendered HTML as a string. It runs on .NET 6 and above (version 2.1.0) and .NET 10 and above (version 3.1.0), and it is maintained by GitHub user soundaranbu, with 381 stars on the repository. The library works in Console, API, MVC, Worker Service, WPF, WinForms, and Azure Functions applications.
Key Features
- RenderAsync and RenderPartialAsync — Render a view with or without a layout, passing a model and a combined ViewData/ViewBag dictionary; the methods return the HTML string asynchronously.
- Try variants — TryRenderAsync and TryRenderPartialAsync return a tuple of bool and string instead of throwing when the view path does not exist.
- Full MVC Razor feature support — ViewModel, ViewBag, ViewData, Layouts, ViewStarts, ViewImports, Partial Views, Tag Helpers, View Components, View Localization (MVC only), and dependency injection into views are all supported.
- Fast rendering — The published benchmark shows a mean render time of 28.73 μs with 24 KB allocated per render, with subsequent renders faster than the first due to initialization.
- Razor Class Library support — Views can be placed in a separate shared RCL project; the .csproj must include AddRazorSupportForMvc and reference Microsoft.AspNetCore.App.
- Dependency injection — Register services with AddRazorTemplating() in Program.cs, then inject either the static RazorTemplateEngine or the IRazorTemplateEngine interface into your classes.
- Limitations — @Url.ContentUrl and @Url.RouteUrl are not supported; runtime compilation of views is not supported starting with .NET 10, so views must be built into the project.
Who should use Razor.Templating.Core?
- .NET backend developers — Generate HTML email content from Razor views without spinning up a full web host, using layouts and partials for consistent branding.
- Console and service developers — Render report HTML or notification bodies in Worker Services, console apps, or Azure Functions, where MVC is not available.
- Team library maintainers — Package reusable Razor views into a Razor Class Library and share them across multiple projects that all call the same rendering API.
What can you do with Razor.Templating.Core?
- Email templating — Create .cshtml email templates with ViewBag and models, then render them to string and pass to SMTP or a service like SendGrid.
- Report generation — Build HTML reports using Tag Helpers, View Components, and partial views, then save the string to a file or return it from an API endpoint.
- HTML for non-web contexts — Render dynamic HTML in desktop apps (WPF, WinForms) or background jobs for use in print previews or embedded web views.
How does it work?
Install the NuGet package and add your .cshtml files to the project. Then call the static RazorTemplateEngine.RenderAsync("/Views/ExampleView.cshtml", model, viewData) method, optionally registering dependencies with AddRazorTemplating() first. The library uses precompiled Razor views from the Razor SDK, so the view files are compiled at build time and rendered at runtime without a web host.
FAQ
Is Razor.Templating.Core free?
The library is free and open-source, distributed as a NuGet package with no paid tiers. The repository includes a GitHub Sponsor button to support development, but all features are available without payment.
What .NET versions are supported?
Supported application types include .NET 6 and above with version 2.1.0, and .NET 10 and above with version 3.1.0. Older .NET versions are documented in the project wiki under historic versions.
Can I use views from a separate library project?
Yes. Place .cshtml files in a Razor Class Library that sets AddRazorSupportForMvc to true and references Microsoft.AspNetCore.App. The library project must be referenced by the application that invokes RenderAsync or a related method.
Does it support runtime compilation?
No, runtime compilation is not supported. Starting with .NET 10, the Razor runtime compilation APIs are marked obsolete and expected to be removed from ASP.NET Core. Views must be included in the project and compiled at build time.
What view features are not supported?
The @Url.ContentUrl and @Url.RouteUrl helpers are not supported in rendered views. The project welcomes contributors to help enable these features.
