Using Razor outside of MVC in .NET Core
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Razor is a templating engine that combines C# with HTML to produce dynamic output. While Razor is most commonly used in ASP.NET MVC views and Razor Pages, it can also be used outside of the web context — for generating HTML emails, PDF templates, code generation, and static site content. The RazorLight library is the most popular choice for standalone Razor rendering, and .NET 6+ also supports RazorEngine through the Razor SDK. This article covers how to render Razor templates in console apps, services, and other non-MVC contexts.
RazorLight — Recommended Library
RazorLight is a standalone Razor engine that works without ASP.NET MVC:
Basic Template Rendering
File-Based Templates
Embedded Resource Templates
HTML Email Generation
The most common use case for Razor outside MVC:
Using ASP.NET Core's Razor Engine Directly
If you already have ASP.NET Core in your project, you can use IRazorViewEngine without RazorLight:
Console Application Example
Dependency Injection Setup
Common Pitfalls
- Template compilation is slow on first run: Razor templates are compiled to C# then to IL on first use. Subsequent renders use the cached compiled template. Pre-compile templates at startup for latency-sensitive applications.
- Thread safety with RazorLight:
RazorLightEngineis thread-safe and should be registered as a singleton. Creating a new engine per request wastes compilation cache and memory. - Model type issues with anonymous objects: Anonymous types are
internalin C#. RazorLight may not be able to access properties across assembly boundaries. Usedynamicor define a concrete model class when templates are in a separate assembly. - Missing Razor SDK in console projects: Console apps do not include the Razor SDK by default. Use RazorLight (which bundles its own compilation) rather than trying to manually configure the ASP.NET Razor SDK in a non-web project.
- HTML encoding by default: Razor automatically HTML-encodes output with
@Model.Value. To output raw HTML, use@Html.Raw(Model.HtmlContent). Forgetting this causes HTML tags to appear as escaped text in emails.
Summary
- Use
RazorLightfor standalone Razor rendering in console apps, services, and background jobs - Templates can be loaded from strings, files, or embedded resources
- HTML email generation is the most common non-MVC use case for Razor
- Register
RazorLightEngineas a singleton for template compilation caching - Use
@Html.Raw()for raw HTML output — Razor HTML-encodes by default
Related reading
- ''using'' statement vs ''try finally''
- ''using'' statement vs ''try finally''
- Using the field of an object as a generic Dictionary key
- Using ThreadPool.QueueUserWorkItem in ASP.NET in a high traffic scenario
- Using TPL Tasks with HttpWebRequest
- Using WebBrowser in a console application
- Various ways of asynchronously returning a collection with C 7 features
- VB.NET equivalent of C As

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.