Razor View Engine

JasonGoemaat edited this page Jul 14, 2011 · 7 revisions

Installing Razor Simply reference Nancy.ViewEngines.Razor.dll and return views ending in cshtml or vbhtml it's that simple. Please note that as of yet certain feautures you might be used to from MVC/ASP.NET are unavailable such as strongly typed views, layouts and renderpartial. You can sorta mock strongly typing views, for those of us who have complex object graphs, by specifing

@MyType model = (MyType)Model;

at the top of your view.

Configuring Razor.

You can specify assemblies and default namespaces that razor needs to use whilst compiling the views by bootstrapping your own IRazorConfiguration implementation. This step is totally optional if you don't require additional references or namespaces in your view.

The default RazorConfiguration looks in app or web.config in the razor section.

Step 1: Create a custom configSection

	<section name="razor" type="Nancy.ViewEngines.Razor.RazorConfigurationSection, Nancy.ViewEngines.Razor" />

Step 2: Configure Razor!

<razor disableAutoIncludeModelNamespace="false">
		<add assembly="System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
		<add assembly="SolrNet" />
		<add assembly="SyslogServerLibrary" />
		<add namespace="SolrNet" />
		<add namespace="SyslogServerLibrary" />

Pretty self explanatory except disableAutoIncludeModelNamespace which by default auto references the assembly of the model you pass into the view.

Use Razor!

Inside NancyModule Constructor

Get["/Home/Index"] = param =>
    var model = repo.GetSomeModel();
    return View["/MyViewPath/Index.cshtml",model];

Note that the path for the razor view is relative to the root of your project.