Written on 8/28/2014 in Web development

The poet part 2 - Razor with NancyFx


The poet NancyFx

Today I will continue to rant about developing 'The Poet'. See my previous post for an explanation about the project. In part 2 I will be setting up NancyFx to use the Razor Viewengine. The reasoning behind this is that I need the razor viewengine to get SquishIt up and running. More about setting up SquishIt in the next post...

Setting up Nancy to use Razor

The first thing to do is installing the Nancy Razor nuget package. You can do this in the nuget package manager or through the package manager console:

Install-Package Nancy.ViewEngines.Razor

This will download the assembly, and update the web.config. The following sections should have been added:

<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
    <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>

And

<buildProviders>
    <add extension=".cshtml" type="Nancy.ViewEngines.Razor.BuildProviders.NancyCSharpRazorBuildProvider, Nancy.ViewEngines.Razor.BuildProviders" />
    <add extension=".vbhtml" type="Nancy.ViewEngines.Razor.BuildProviders.NancyVisualBasicRazorBuildProvider, Nancy.ViewEngines.Razor.BuildProviders" />
</buildProviders>

And

<system.web.webPages.razor>
    <pages pageBaseType="Nancy.ViewEngines.Razor.NancyRazorViewBase">
    <namespaces>
        <add namespace="Nancy.ViewEngines.Razor" />
    </namespaces>
    </pages>
</system.web.webPages.razor>

This essentially tells nancy to use the razor viewengine. An interesting thing to mention is the 'pageBaseType="Nancy.ViewEngines.Razor.NancyRazorViewBase"', this will define the viewbase that will be used. When you use 'Model', for example inside your view, you can do this because the viewbase has a definition for 'Model'. So you can also create your own implementation of a viewbase and set it up here.

Getting intellisense working

If you start using Razor now like you used to in MVC, you'll get a ton of errors. The first you'll see is that there is no intellisense, you can't access the model, the 'RenderBody()' function, or actually anything you would want. To get this to work, you need to add the following to the top of your razor view:

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase

This will set the viewbase on the current view, I believe this is only for intellisense2, you can just type everything by hand without this statement and it will probably work. The default viewbase uses a model of type dynamic, if you have a strongly typed model, you can tell razor about it like this:

@inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<YourType>

Razor assemblies and namespaces

Which brings me up to the next point, assemblies and namespaces. Nancy razor out of the box is stupid, it doesn't know which assemblies to use. You have to define it by editing the the web.config to look like this:

<configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
        <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
    <section name="razor" type="Nancy.ViewEngines.Razor.RazorConfigurationSection, Nancy.ViewEngines.Razor" /><!-- Add this -->
</configSections>
<razor disableAutoIncludeModelNamespace="false"><!-- And this -->
    <assemblies>
        <add assembly="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </assemblies>
    <namespaces>
        <add namespace="System.Linq" />
    </namespaces>
</razor>

This will effectively tell razor to use assemblies xyz and namespaces abc when building views. Without the System reference, you can't use 'String'. If you don't define the Linq namespace, you can't use linq... If you are using strongly typed viewmodels, you also need to add their assembly to get them to work. Once this is done, you can start using the razor view engine. I haven't gotten to the bottom of how you can get it working cleaner and better, but I hope this saves everyone a bit of time. I lost quite a bit of time getting this to work by combining multiple sources into a single solution.

Razor for Nancy documentation

Newer Older Top
blog comments powered by Disqus