Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.In addition up to my previous post, Using ASP.NET Core Razor Views from a Class Library, I wanted to also dig into how you can use Razor in a Console Application. Basically from outside of ASP.NET Core MVC. The most likely situation for this is wanting to generate Emails using Razor that are being sent from a service or console application. The other might be from another web framework/middleware inside of ASP.NET Core. To demo how to use Razor outside of ASP.NET CORE MVC, I’m going to use Razor in a Console Application. Specifically I’m using netcoreapp2.1 for this demo but you can use anything netstandard2.0. If you’re looking at rendering Razor views from ASP.NET Core MVC for the purpose of generating email content, I recommend checking out Scott Sauber’s blog post: Walkthrough: Creating an HTML Email Template with Razor and Razor Class Libraries and Rendering it from a .NET Standard Class Library.
RazorViewStringRendererThe heart of the code is actually from RazorViewToStringRenderer.cs in the aspnet/Entropy repo on GitHub. This repo is for:
A chaotic experimental playground for new features and ideas – check here for small and simple samples for individual features.
NuGet PackagesIn our Console App, you’ll need the following packages:
<PackageReference Include=”Microsoft.AspNetCore.Hosting” Version=”2.1.1″ /> <PackageReference Include=”Microsoft.Extensions.FileProviders.Physical” Version=”2.1.1″ /> <PackageReference Include=”Microsoft.Extensions.PlatformAbstractions” Version=”1.1.0″ />