NancyFx/Nancy — pull request #1167 by lukesmith (2013-07-28 21:50:10+00:00)
Title: Nancy render razor app code helpers

This is a stab at getting the Nancy Razor view engine to render `@helper`s that are present in the `App_Code` directory, which can be [used across multiple views](http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx).

The main changes to nancy were
- extracting the `NancyRazorEngineHostFactory` which creates either the `NancyRazorEngineHost` or the new `NancyRazorCodeEngineHost` depending on whether the view location is in `App_Code` or not.
- Adding `NancyRazorCodeBase` as the base class for the app_code helpers (extracted shared methods from `NancyRazorViewBase` into `NancyPageBuilder`).

The build providers need to be referenced in the web.config In order to get the `@helper` files compiled.

The `@helper` methods compile to static methods so I couldn't see a way to expose access to the `Model` and `HtmlHelper` instances. For reference the generated class

```
Line 12: namespace RazorOutput {
Line 13:
Line 14:
Line 15: public class CodeHelper : Nancy.ViewEngines.Razor.NancyRazorCodeBase {
Line 16:
Line 17: #line hidden
Line 18:
Line 19: #line default
Line 20: #line hidden
Line 21: public static Nancy.ViewEngines.Razor.HelperResult Render(string text)
Line 22: {
Line 23: #line default
Line 24: #line hidden
Line 25: return new Nancy.ViewEngines.Razor.HelperResult(__razor_helper_writer => {
Line 26:
Line 27:
Line 28: WriteLiteralTo(__razor_helper_writer, "

");
Line 29:
Line 30: WriteTo(__razor_helper_writer, text);
Line 31:
Line 32: WriteLiteralTo(__razor_helper_writer, "

\r\n");
Line 33:
Line 34:
Line 35: });
Line 36:
Line 37: #line default
Line 38: #line hidden
Line 39: }
Line 40: #line default
Line 41: #line hidden
Line 42:
Line 43:
Line 44: public CodeHelper() {
Line 45: }
Line 46: }
Line 47: }
Line 48:
```