Razor.Templating.Core 2.0.0

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

// Install Razor.Templating.Core as a Cake Tool
#tool nuget:?package=Razor.Templating.Core&version=2.0.0                


Build+Test Nuget Downloads Coverage

Using Razor for HTML templating was never been so easy like this. Render your .cshtml files into string easily using this library.

This project makes use of Razor SDK for precompiling the views.

Supported Application Types

.NET Core 3.0 .NET Core 3.1 .NET 5 > .NET 6
Preferred Version v1.6.0 v1.6.0 v1.6.0 2.0.0
Worker Service
Azure Functions

Supported View Features

MVC Razor View Features
Partial Views
Tag Helpers
View Components (.NET 5 +)
Dependency Injection into Views

**Contributors are welcome who can help to enable these unsupported features.


  • Email Templating
  • Report Generation & more

Installing Nuget Package

This library is available as Nuget package

Using .NET CLI

dotnet add package Razor.Templating.Core

Using Package Reference .csproj

<PackageReference Include="Razor.Templating.Core" Version="2.0.0" />

Usage - Render View With Layout

To render a view with layout, model, viewdata or viewbag, then call the RenderAsync() on the RazorTemplateEngine static class

RenderAsync() method

using Razor.Templating.Core;

var model = new ExampleModel()
    PlainText = "This text is rendered from Razor Views using Razor.Templating.Core",
    HtmlContent = "<em>You can use it to generate email content, report generation and so on</em>"

// Both ViewBag and ViewData should be added to the same dictionary. 
var viewDataOrViewBag = new Dictionary<string, object>();
// ViewData is same as mvc
viewDataOrViewBag["Value1"] = "1";

// ViewBag.Value2 can be written as below. There's no change on how it's accessed in .cshtml file
viewDataOrViewBag["Value2"] = "2";

var html = await RazorTemplateEngine.RenderAsync("/Views/ExampleView.cshtml", model, viewDataOrViewBag);

Before applying this code, follow this article for sample implementation: https://medium.com/@soundaranbu/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79

Render View Without Layout

In case if there's a need to render a view without layout, use RenderParitalAsync() method.

RenderPartialAsync() method

var html = await RazorTemplateEngine.RenderPartialAsync("/Views/ExampleView.cshtml", model, viewDataOrViewBag);

Render Views Without Throwing Exception

There are TryRenderAsync() and TryRenderPartialAsync methods which will not throw exception if the view doesn't exist. Instead they return a tuple to indicate whether the view exists and the rendered string.

TryRenderAsync() method

var (viewExists, renderedView) = await engine.TryRenderAsync("~/Views/Feature/ExampleViewWithoutViewModel.cshtml");

TryRenderPartialAsync() method

var (viewExists, renderedView) = await engine.TryRenderPartialAsync("~/Views/_ExamplePartialView.cshtml", model);

Razor Views in Library

We can organize the Razor view files(.cshtml) in a separate shared Razor Class Libary(RCL). Please find a sample library here

The Razor Class Library's .csproj file should look something like below. Whereas, AddRazorSupportForMvc property is mandatory.

Also, RCL should be referenced by the main project or where any of the rendering methods like RazorTemplateEngine.RenderAsync() are invoked.

<Project Sdk="Microsoft.NET.Sdk.Razor">

    <FrameworkReference Include="Microsoft.AspNetCore.App" />

Dependency Injection

Dependencies can be injected directly into views using @inject in .csthml file. Refer sample application here

In ASP.NET Core, add dependency like below in Startup.cs -> ConfigureServices

//add after registering all the dependencies

or in console or other applications, add as below

using Microsoft.Extensions.DependencyInjection;

// Add dependencies to the service collection
var services = new ServiceCollection();
// Add RazorTemplating after registering all dependencies
// this is important for the razor template engine to find the injected services

Once the dependencies are registered, we can use either one of these ways:

Using RazorTemplateEngine static class

var html = await RazorTemplateEngine.RenderAsync("~/Views/ExampleViewServiceInjection.cshtml");

Using IRazorTemplateEngine

  • Instead of using the RazorTemplateEngine static class, it's also possible to use the IRazorTemplateEngine interface to inject dependency directly into the constructor.
public class MyService {
    private readonly IRazorTemplateEngine _razorTemplateEngine;

    public MyService (IRazorTemplateEngine razorTemplateEngine)
        _razorTemplateEngine = razorTemplateEngine;

    public async Task Index()
        var renderedView = await _razorTemplateEngine.RenderAsync("/Views/Home/Index.cshtml");
        // do something with renderedView

How to render razor views from absolute path

We can make use of ASP.NET Core's inbuilt RazorRuntimeCompilation to render any .cshtml inside or outside of the project.

As of v1.7.0+, we can achieve this as below:

using Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Razor.Templating.Core;

var services = new ServiceCollection();
services.Configure<MvcRazorRuntimeCompilationOptions>(opts =>
    opts.FileProviders.Add(new PhysicalFileProvider(@"D:\PathToRazorViews")); // This will be the root path

var html = await RazorTemplateEngine.RenderAsync("/Views/Home/Rcl.cshtml"); // relative path to the root

Please note this may become slightly better in the future versions of our library.


  • Please ensure that the views path is always unique among all the shared template projects.

Sample Applications

Please find the sample applications here


If you find this helpful, consider supporting the development of this library by sponsoring one or more coffee ;) Thanks!

alternate text is missing from this package README image


Thanks to all the great articles and projects which helped to bring this library out!

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages (18)

Showing the top 5 NuGet packages that depend on Razor.Templating.Core:

Package Downloads

Sitko.Core is a set of libraries to help build .NET Core applications fast


Application Component for the Alliance Business Suite.


Package Description


The official integration between Digizuite DAM and Optimizely CMS.


Package Description

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on Razor.Templating.Core:

Repository Stars
Sitko.Core is a set of libraries to help build .NET Core applications fast
Version Downloads Last updated
2.0.0 88,814 4/18/2024
2.0.0-rc.1 1,692 3/4/2024
1.9.0 472,169 8/25/2023
1.9.0-rc.2 84,944 2/25/2023
1.9.0-rc.1 128,979 11/13/2022
1.8.0 928,370 11/5/2022
1.8.0-rc.1 6,667 9/17/2022
1.7.1 478,113 7/2/2022
1.7.1-rc.1 14,142 2/20/2022
1.7.0 718,935 11/13/2021
1.7.0-preview.1 2,182 9/26/2021
1.6.1-rc.1 5,271 1/13/2022
1.6.0 427,589 9/26/2021
1.6.0-rc.2 7,242 5/14/2021
1.6.0-rc.1 690 5/13/2021
1.5.0 376,516 2/27/2021
1.4.0 98,258 12/5/2020
1.4.0-rc.1 629 11/19/2020
1.3.0 8,579 11/17/2020
1.2.1 32,776 9/21/2020
1.2.0 3,699 7/11/2020
1.1.2 2,726 4/13/2020
1.1.0 873 4/13/2020
1.0.1 935 4/3/2020
1.0.0 1,074 4/3/2020

- Added TryRenderAsync() and TryRenderPartialAsync() methods to render the views without throwing exceptions
     - Removed obselete methods RazorTemplateEngine.RenderAsync() generic method and RazorTemplateEngine.Initialize()