Razor.Templating.Core 1.7.1-rc.1

.NET 6.0
This is a prerelease version of Razor.Templating.Core.
Install-Package Razor.Templating.Core -Version 1.7.1-rc.1
dotnet add package Razor.Templating.Core --version 1.7.1-rc.1
<PackageReference Include="Razor.Templating.Core" Version="1.7.1-rc.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Razor.Templating.Core --version 1.7.1-rc.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Razor.Templating.Core, 1.7.1-rc.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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=1.7.1-rc.1&prerelease

// Install Razor.Templating.Core as a Cake Tool
#tool nuget:?package=Razor.Templating.Core&version=1.7.1-rc.1&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

<img src="https://raw.githubusercontent.com/soundaranbu/RazorTemplating/master/src/Razor.Templating.Core/assets/icon.png" width="70" height="70" />

Razor Templating

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 v1.7.1-rc.1
Console
Api
Mvc
Worker Service
WPF
WinForms
Azure Functions

Supported View Features

MVC Razor View Features
ViewModel
ViewBag
ViewData
Layouts
ViewStarts
ViewImports
Partial Views
Tag Helpers
View Components (.NET 5 +)
Dependency Injection into Views

Applications

  • Email Templating
  • Report Generation & so on

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="1.7.0-rc.1" />

Simple Usage:

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 working implementation: https://medium.com/@soundaranbu/render-razor-view-cshtml-to-string-in-net-core-7d125f32c79

Dependency Injection [Since v1.4.0]

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

...
services.AddTransient<ExampleService>();
//add after registering all the dependencies
services.AddRazorTemplating();

or in console or other applications, add as below

using Microsoft.Extensions.DependencyInjection;

// Add dependencies to the service collection
var services = new ServiceCollection();
services.AddTransient<ExampleService>();
// Add RazorTemplating after registering all dependencies
// this is important for the razor template engine to find the injected services
services.AddRazorTemplating(); 
var html = await RazorTemplateEngine.RenderAsync("~/Views/ExampleViewServiceInjection.cshtml");

Razor Views in Library

Razor view files(.cshtml) can be organized in a separate shared Razor Class Libary(RCL). Sample library can be found here

Sample Applications

Please find the sample applications here

Note:

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

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

Product Versions
.NET net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

NuGet packages (8)

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

Package Downloads
Sitko.Core.App.Web

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

FenixAlliance.ACL.Dependencies

Application Component for the Alliance Business Suite.

Unlog.AspNetCore

the aspnetcore package for UnLog

Dotented

Package Description

Digizuite.Optimizely

The official integration between Digizuite DAM and Optimizely CMS.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.7.1-rc.1 1,267 2/20/2022
1.7.0 117,802 11/13/2021
1.7.0-preview.1 2,099 9/26/2021
1.6.1-rc.1 103 1/13/2022
1.6.0 70,058 9/26/2021
1.6.0-rc.2 4,963 5/14/2021
1.6.0-rc.1 396 5/13/2021
1.5.0 199,045 2/27/2021
1.4.0 48,356 12/5/2020
1.4.0-rc.1 541 11/19/2020
1.3.0 7,544 11/17/2020
1.2.1 19,699 9/21/2020
1.2.0 3,152 7/11/2020
1.1.2 1,169 4/13/2020
1.1.0 355 4/13/2020
1.0.1 548 4/3/2020
1.0.0 513 4/3/2020

- Fixed assembly loading issue for WPF applications