Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Razor View Engine in a console application?

My console app needs to send HTML emails. I'd like to write the emails in HTML format in a Razor view and have the engine generate the email body content.

This means no controllers or requests. How could I go about this?

like image 374
Roman Avatar asked Feb 24 '12 05:02

Roman


People also ask

What is the Razor view engine in asp net?

Razor View Engine is a markup syntax which helps us to write HTML and server-side code in web pages using C# or VB.Net. It is server-side markup language however it is not at all a programming language.

How do I run a Razor page in Visual Studio code?

Open the integrated terminal. Change to the directory ( cd ) which will contain the project. The dotnet new command creates a new Razor Pages project in the RazorPagesMovie folder. The code command opens the RazorPagesMovie folder in the current instance of Visual Studio Code.

How do I run a console application in Visual Studio?

Build and run your code in Visual Studio To run the code, on the menu bar, choose Debug, Start without debugging. A console window opens and then runs your app. When you start a console app in Visual Studio, it runs your code, then prints "Press any key to continue . . ." to give you a chance to see the output.


1 Answers

There is an open source project which allows to use Razor as a general templating engine: it's called RazorEngine (the code in on GitHub)

A sample for the project's page:

string template = "Hello @Model.Name! Welcome to Razor!"; string result = Razor.Parse(template, new { Name = "World" }); 
like image 93
nemesv Avatar answered Sep 24 '22 11:09

nemesv