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?
Typically, Razor rendering in ASP.NET MVC is reserved purely for view rendering and generation of HTML output as part of an MVC request.
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.
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? 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.
What's the current best solution for generating HTML from ASP.NET Razor templates within a Console Application?
RazorEngine
. Full stop.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With