ServiceStack’s Markdown Razor Engine. Wow.

ServiceStack is a pretty sweet-looking alternative to WCF. It provides strongly-typed, well-designed, REST/RCP+SOAP services for .NET and Mono. Check out the README in the repository to see how ridiculously easy it is to setup a service. Unfortunately, I haven’t yet had a chance to use the stack. I was looking through the code today and saw a Markdown-Razor Engine.

I was like, “Wha….?

Yes, they offer an alternative view engine which blends Markdown and Razor. These are my two favorite syntaxes, so I had to see how well it compiles.

I compiled the ServiceStack.RazorEngine from source (in Ubuntu 12.04/Mono, no less!) to test a simple markdown file.

I created this markdown file,, to throw at the framework.

# Example ServiceStack.Markdown

## Showing @examples.Count items

@foreach (var item in examples) {
  - @item.Name: @item.Number 

**Note: The template requires a space after the item.Number value**

For more information, check out 
[the docs](

Also, *don't forget* to check out the code at 

Here are some _other_ attempts to **break** 
the markdown **generation_of_html**. **escaped\_underscore\_in\_tags**

A space after double-asterisk (<strong> tags) will ** break **

Now, when I say simple, I mean it has most elements of markdown that I use regularly. It also uses the markdown-razor foreach syntax which iterates over a collection of Example objects. This view is not strongly-typed, so this is equivalent to a dynamic Razor view.

The code to compile this view is really simple. You create the template engine, and provide a markdown page object which takes a full path (.e.g /fakepath/Debug) and the template contents (e.g. the above markdown snippet).

// Create the markdown-razor template compiler
MarkdownFormat format = new MarkdownFormat();
string contents = File.ReadAllText(template);
var page = new MarkdownPage(format, path, "example", contents );

Then, you create your view object (ViewBag?).

// Create our view container (ViewBag)
var view = new Dictionary<string, object>() 
	{ "examples", examples }

All that’s left is to pass in the view object and compile it to html!

// Compile and output. 
// This can be redirected to html file 
// e.g. RazorExample.exe > output.html
var html = format.RenderDynamicPageHtml("example", view);

The Code

As always, the code for this post is available on github. More examples on the Markdown Razor syntax available in ServiceStack are available in the test project in ServiceStack’s repository.

Related Articles