Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the current best solution for generating HTML from ASP.NET Razor templates within a Console Application?

I want to do this:

string template = "Hello @Model.Name! Welcome to Razor!"; string result = Razor.Parse(template, new { Name = "World" }); 

And it appears that http://razorengine.codeplex.com is perfect, except it's a year old.

EDIT: Turns out that RazorEngine has moved to GitHub and had a commit a few months back: https://github.com/Antaris/RazorEngine

I noticed that Service Stack has some Razor self-hosting but while there's a long page here http://razor.servicestack.net there's no "hello world you can totally do this from a console."

What's the current best solution for generating HTML from ASP.NET Razor templates within a Console Application?

like image 775
Scott Hanselman Avatar asked Jan 09 '13 17:01

Scott Hanselman


People also ask

Which ASP.NET MVC object generates the HTML output that displays in web browser?

Typically, Razor rendering in ASP.NET MVC is reserved purely for view rendering and generation of HTML output as part of an MVC request.

Is Razor pages better than MVC?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

What is Razor in HTML?

Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a . cshtml file extension.

What is Razor in Visual Studio?

What is Razor? Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages. Server-based code can create dynamic web content on the fly, while a web page is written to the browser.


2 Answers

What's the current best solution for generating HTML from ASP.NET Razor templates within a Console Application?

RazorEngine. Full stop.

like image 105
Darin Dimitrov Avatar answered Oct 02 '22 19:10

Darin Dimitrov


ServiceStack is another option for rendering Razor view pages. Although it's optimized for integration into a ASP.NET or HttpListener Web Host (and provides API's for auto-discovering and registering view pages in a directory, re-compiling modified pages on the fly, etc), it also supports static generation of view pages:

var razor = new RazorFormat {     VirtualPathProvider = new InMemoryVirtualPathProvider(new BasicAppHost()),     EnableLiveReload = false, //don't scan for file system for changes }.Init();  var page = razor.CreatePage("Hello @Model.Name! Welcome to Razor!"); var html = razor.RenderToHtml(page, new { Name = "World" }); html.Print(); 

Here's the stand-alone unit test of this example.

The benefits of using ServiceStack's Razor view rendering engine includes access to many of the MVC's HtmlHelpers that were ported to ServiceStack. You can also easily host a razor website from a self-hosted ServiceStack HttpListener as seen in razor-console.servicestack.net, the source code of which is available in a Self-Hosted Console Application or Windows Service.

like image 33
mythz Avatar answered Oct 02 '22 21:10

mythz