Skip to content

Razor Templating Engine to render Razor Views(.cshtml files) to String in Console, Web, Service, Desktop workloads in .NET Core 3+

License

Notifications You must be signed in to change notification settings

TheAxeDude/RazorTemplating

 
 

Repository files navigation

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
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.6.0-rc.2" />

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>"
};

var viewData = new Dictionary<string, object>();
viewData["Value1"] = "1";
viewData["Value2"] = "2";

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

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!

About

Razor Templating Engine to render Razor Views(.cshtml files) to String in Console, Web, Service, Desktop workloads in .NET Core 3+

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • HTML 98.4%
  • C# 1.6%