55

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?

Scott Hanselman
  • 17,712
  • 6
  • 74
  • 89

4 Answers4

39

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

RazorEngine. Full stop.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    Would you recommend any blog that goes step by step to get this work? I have been looking last an hour and this is the only link I found http://mehdi.me/generating-html-emails-with-razorengine-introduction/#start. I followed the tutorial but getting error saying Errors while compiling a Template. – akd Feb 12 '15 at 16:50
  • 2
    FYI: "This project is searching for new maintainers, so if you want to help write on gitter or start sending PRs :)" – Chris Weber Dec 09 '16 at 18:03
14

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.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • 2
    ServiceStack has gone commercial; if you're not writing open source you will likely have to license it. Older versions are BSD; newer are GNU Affero GPL (unless licensed). See https://servicestack.net/download and https://servicestack.net/pricing – TrueWill Jul 07 '15 at 17:48
2

Nancy has a self-host option and an ability to plug Razor as a view engine.

https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-wcf

https://github.com/NancyFx/Nancy/wiki/Razor-View-Engine

Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
0

I wouldn't call this the "current best" solution. However, I found it quite interesting and it will let you accomplish what you are trying to do. It just isn't very neatly wrapped up. http://vibrantcode.com/blog/2010/11/16/hosting-razor-outside-of-aspnet-revised-for-mvc3-rc.html/

Scott Stevens
  • 390
  • 1
  • 7
  • Thanks, looks useful, but it seems like either RazorEngine or @MikeSW's pointer to RazorMachine is best. – Scott Hanselman Jan 09 '13 at 20:53
  • link is broken, actual link withoul last slash: http://vibrantcode.com/blog/2010/11/16/hosting-razor-outside-of-aspnet-revised-for-mvc3-rc.html Unfortunately, cant edit answer because it requires 6 chars of changes instead on one :) – hal Apr 25 '19 at 06:45