title: How do I get the RazorEngine Parse method to work successfully with the layout?
body: <p>I'm using the <a href="http://razorengine.codeplex.com/" rel="nofollow">RazorEngine</a> and the Parse method throws an error if I specify the layout in the view. If I don't specify it then the Parse is successful but the layout is not present (it seems to ignore that the layout if specified in _ViewStart.cshtml). Please <del>advise on</del> tell me how do I get the RazorEngine Parse method to work successfully with the layout?</p>
  
  <p>I have included the C#, view, and layout below:</p>
  
  <pre><code>private String GetViewHtmlIncludingLayout(Controller controller, Object model, String view)
  {       
      // this returns the view as string, including its razor markup      
      string template = ControllerBaseExtensions.GetViewTemplate(controller, view);    
      // this converts the razor markup view to html
      string result = Razor.Parse(template, model);
      return result;
  }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@using Carnotaurus.GhostPubsMvc.Web.Models
  @model AboutModel
  @{
      Layout = "~/Views/Shared/_Layout.cshtml";
      ViewBag.Title = "About us";
  }
  
  &lt;!-- Subhead
  ================================================== --&gt;
  &lt;header class="jumbotron subhead"&gt;
      &lt;div class="container"&gt;
          &lt;div&gt;
              &lt;h1&gt;@Model.JumboTitle&lt;/h1&gt;
              &lt;p class="lead"&gt;
                  About ghost pubs.com
              &lt;/p&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/header&gt;
  
  &lt;div class="container"&gt;
  
      &lt;!-- Docs nav
      ================================================== --&gt;
      &lt;div class="row"&gt;
          &lt;div class="span4"&gt;
              &lt;div&gt;
                  &lt;div&gt;
                      &lt;img class="img-circle face-relative" src="~/Content/bootstrap/2.3.1/img/ghostpubs/padded-logo-125x125.png" alt="Ghost Pubs" title="Ghost Pubs" /&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="span8"&gt;
  
              &lt;!-- Well
            ================================================== --&gt;
              &lt;section id="well"&gt;
                  &lt;div class="page-header"&gt;
                      &lt;h1&gt;All about Ghost Pubs dot Com&lt;/h1&gt;
                  &lt;/div&gt;
                  &lt;h2&gt;Let's tell you who we are&lt;/h2&gt;
                  &lt;div class="well well-large"&gt;
                      &lt;p&gt;
                          We are chaps involved in running &lt;a href="http://www.myPubGuide.com"&gt;myPUBGUIDE.com&lt;/a&gt;.
      We have noticed that almost sixty public houses close each month. Therefore, given
      the success of myPUBGUIDE, we thought it would be a good idea to launch a similar
      website for drinking establishments that are haunted, closed, abandoned, and demolished.
                      &lt;/p&gt;
                      &lt;p&gt;
                          Clearly such establishments do not belong on an active pub search. These deserve
      a special website of their own, where your contribution would be encouraged. We
      intend to launch this website in early 2014. We are sure that you have a ghost story
      to share with us about your old local.
                      &lt;/p&gt;
                      &lt;p&gt;
                          As the name suggests Ghost Pubs we are looking for any information you can give
      regarding any hauntings or paranormal phenomena regarding your local pub.
                      &lt;/p&gt;
                  &lt;/div&gt;
  
                  &lt;hr&gt;
              &lt;/section&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>Layout</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" /&gt;
      &lt;meta name="viewport" content="width=device-width" /&gt;
      @Styles.Render("~/Content/css")
      @Scripts.Render("~/bundles/modernizr")
      &lt;!-- styles --&gt;
      &lt;link href="@Url.Content("~/content/bootstrap/2.3.1/css/bootstrap.css")" rel="stylesheet"&gt;
      &lt;link href="@Url.Content("~/content/bootstrap/2.3.1/css/bootstrap-responsive.css")" rel="stylesheet"&gt;
      &lt;link href="@Url.Content("~/content/bootstrap/2.3.1/css/docs.css")" rel="stylesheet"&gt;
      &lt;link href="@Url.Content("~/content/bootstrap/2.3.1/css/ghostpubs/docs2.css")" rel="stylesheet"&gt;
      &lt;link href="@Url.Content("~/content/bootstrap/2.3.1/css/prettify.css")" rel="stylesheet"&gt;
      &lt;link href="@Url.Content("~/content/bootstrap/2.3.1/font-awesome/css/font-awesome.css")" rel="stylesheet"&gt;
      &lt;link href="@Url.Content("~/content/bootstrap/2.3.1/css/bootswatch.css")" rel="stylesheet"&gt;
  &lt;/head&gt;
  &lt;body data-spy="scroll" data-target=".bs-docs-sidebar"&gt;
  
      &lt;!-- Navbar
      ================================================== --&gt;
      &lt;div class="navbar navbar-inverse navbar-fixed-top"&gt;
          &lt;div class="navbar-inner"&gt;
              &lt;div class="container"&gt;
                  &lt;button type="button" class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse"&gt;
                      &lt;span class="icon-bar"&gt;&lt;/span&gt;
                      &lt;span class="icon-bar"&gt;&lt;/span&gt;
                      &lt;span class="icon-bar"&gt;&lt;/span&gt;
                  &lt;/button&gt;
                  &lt;a class="brand" href="/home/index"&gt;GhostPubs.com&lt;/a&gt;
                  &lt;div class="nav-collapse collapse"&gt;
                      &lt;ul class="nav"&gt;
                          &lt;li&gt;
                              &lt;a href="/home/index"&gt;Home&lt;/a&gt;
                          &lt;/li&gt;
                          &lt;li&gt;
                              &lt;a href="/home/index"&gt;News&lt;/a&gt;
                          &lt;/li&gt;
                          &lt;li&gt;
                              &lt;a href="/home/index"&gt;Search&lt;/a&gt;
                          &lt;/li&gt;
                          &lt;li&gt;
                              &lt;a href="/home/about"&gt;About us&lt;/a&gt;
                          &lt;/li&gt;
                          &lt;li class=""&gt;
                              &lt;a href="#" style="display: none"&gt;Not wired-up&lt;/a&gt;
                          &lt;/li&gt;
                          &lt;li&gt;
                              &lt;a href="/bootstrap/index" style="display: none"&gt;Bootstrap example - Home&lt;/a&gt;
                          &lt;/li&gt;
                          &lt;li&gt;
                              &lt;a href="/bootstrap/javascript" style="display: none"&gt;Bootstrap example- JavaScript&lt;/a&gt;
                          &lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      @RenderBody()
  
      &lt;!-- Footer
      ================================================== --&gt;
      &lt;footer class="footer"&gt;
          &lt;div class="container"&gt;
              &lt;span class="label label-info"&gt;Designed and developed by &lt;a href="http://carnotaurus.philipcarney.com" target="_blank"&gt;Philip Carney&lt;/a&gt; &lt;/span&gt;
          &lt;/div&gt;
      &lt;/footer&gt;
  
      @Scripts.Render("~/bundles/jquery")
      @RenderSection("scripts", required: false)
  
      &lt;!-- Le javascript
      ================================================== --&gt;
      &lt;!-- Placed at the end of the document so the pages load faster --&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap.js")"&gt;&lt;/script&gt;
      &lt;script type="text/javascript" src="http://platform.twitter.com/widgets.js"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/jquery.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap-transition.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap-alert.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap-modal.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap-dropdown.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap-scrollspy.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap-tab.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap-tooltip.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap-popover.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap-button.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap-collapse.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap-carousel.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap-typeahead.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/bootstrap-affix.js")"&gt;&lt;/script&gt;
  
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/holder.js")"&gt;&lt;/script&gt;
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/prettify.js")"&gt;&lt;/script&gt;
  
      &lt;script src="@Url.Content("~/content/bootstrap/2.3.1/js/application.js")"&gt;&lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The error is: Object reference not set to an instance of an object.</p>
  
  <p>There is no inner error but the stack trace says:</p>
  
  <pre><code>  at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context) in c:\_git\RazorEngine\src\Core\RazorEngine.Core\Templating\TemplateBase.cs:line 139
     at RazorEngine.Templating.TemplateService.Run(ITemplate template, DynamicViewBag viewBag) in c:\_git\RazorEngine\src\Core\RazorEngine.Core\Templating\TemplateService.cs:line 608
     at RazorEngine.Templating.TemplateService.Parse(String razorTemplate, Object model, DynamicViewBag viewBag, String cacheName) in c:\_git\RazorEngine\src\Core\RazorEngine.Core\Templating\TemplateService.cs:line 439
     at RazorEngine.Razor.Parse(String razorTemplate, Object model) in c:\_git\RazorEngine\src\Core\RazorEngine.Core\Razor.cs:line 251
     at Carnotaurus.GhostPubsMvc.Web.Controllers.HomeController.GetViewHtmlIncludingLayout(Controller controller, Object model, String view, String layoutPath) in c:\test\GhostPubsMvc4\Carnotaurus.GhostPubsMvc.Web\Carnotaurus.GhostPubsMvc.Web\Controllers\HomeController.cs:line 59
     at Carnotaurus.GhostPubsMvc.Web.Controllers.HomeController.CreateHtmlPage(AboutModel model, String view, String layoutPath, String targetFilename) in c:\test\GhostPubsMvc4\Carnotaurus.GhostPubsMvc.Web\Carnotaurus.GhostPubsMvc.Web\Controllers\HomeController.cs:line 41
     at Carnotaurus.GhostPubsMvc.Web.Controllers.HomeController.About() in c:\test\GhostPubsMvc4\Carnotaurus.GhostPubsMvc.Web\Carnotaurus.GhostPubsMvc.Web\Controllers\HomeController.cs:line 34
     at lambda_method(Closure , ControllerBase , Object[] )
     at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
     at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;BeginInvokeSynchronousActionMethod&gt;b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
     at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3c()
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;&gt;c__DisplayClass45.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3e()
  </code></pre>