title: Self hosting nancy with razor returns empty body
body: <p>Self hosting nancy with razor returns empty body.</p>
  
  <p>I'm trying to self-host nancy in a console application with razor views, however I'm getting an empty body back.
  It does work when running in IIS.</p>
  
  <p>What I did:</p>
  
  <ul>
  <li>I added all the appropriate razor configuration and build providers to the App.Config</li>
  <li><p>I added a custom static root path provider, which seems to be configured well, as static files are served without a problem:</p>
  
  <pre><code>protected override IRootPathProvider RootPathProvider
  {
      get 
      {
  
          if (ConfigurationManager.AppSettings["RootPath"].HasValue())
          {
              var path = Path.Combine(new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName, ConfigurationManager.AppSettings["RootPath"]); 
  
              return new StaticRootPathProvider(path);
          }
  
          return new DefaultRootPathProvider();            
      }
  }
  </code></pre></li>
  <li><p>The razor view is found an being compiled, because when I add invalid code to the view, I do get a razor compiler error</p></li>
  <li><p>When I debug with all exceptions caught, I get an exception on serving the page:</p></li>
  </ul>
  
  <p>A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Nancy.dll</p>
  
  <p>Additional information: Cannot convert type 'Nancy.Responses.Negotiation.Negotiator' to 'Nancy.Response'</p>
  
  <blockquote>
    <p>Nancy.dll!Nancy.Routing.DefaultRouteInvoker.CastResultToResponse(dynamic result)    Unknown
        Nancy.dll!Nancy.Routing.DefaultRouteInvoker.InvokeRouteWithStrategy(dynamic result, Nancy.NancyContext context) Unknown
        Nancy.dll!Nancy.Routing.DefaultRouteInvoker.Invoke.AnonymousMethod__5(System.Threading.Tasks.Task completedTask)   Unknown
        Nancy.dll!Nancy.Helpers.TaskHelpers.WhenCompleted(System.Threading.Tasks.Task task, System.Action> onComplete, System.Action> onFaulted, bool execSync) Unknown
        Nancy.dll!Nancy.Routing.DefaultRouteInvoker.Invoke(Nancy.Routing.Route route, System.Threading.CancellationToken cancellationToken, Nancy.DynamicDictionary parameters, Nancy.NancyContext context) Unknown
        Nancy.dll!Nancy.Routing.DefaultRequestDispatcher.Dispatch.AnonymousMethod__0(System.Threading.Tasks.Task completedTask) Unknown
        Nancy.dll!Nancy.Helpers.TaskHelpers.WhenCompleted(System.Threading.Tasks.Task task, System.Action> onComplete, System.Action> onFaulted, bool execSync) Unknown
        Nancy.dll!Nancy.Routing.DefaultRequestDispatcher.Dispatch(Nancy.NancyContext context, System.Threading.CancellationToken cancellationToken) Unknown
        Nancy.dll!Nancy.NancyEngine.InvokeRequestLifeCycle.AnonymousMethod__9(System.Threading.Tasks.Task t)    Unknown
        Nancy.dll!Nancy.Helpers.TaskHelpers.WhenCompleted(System.Threading.Tasks.Task task, System.Action> onComplete, System.Action> onFaulted, bool execSync) Unknown
        Nancy.dll!Nancy.NancyEngine.InvokeRequestLifeCycle(Nancy.NancyContext context, System.Threading.CancellationToken cancellationToken, Nancy.Bootstrapper.IPipelines pipelines)   Unknown
        Nancy.dll!Nancy.NancyEngine.HandleRequest(Nancy.Request request, System.Func preRequest, System.Threading.CancellationToken cancellationToken)   Unknown
        Nancy.dll!Nancy.NancyEngineExtensions.HandleRequest(Nancy.INancyEngine nancyEngine, Nancy.Request request, System.Func preRequest)   Unknown
        Nancy.dll!Nancy.NancyEngineExtensions.HandleRequest(Nancy.INancyEngine nancyEngine, Nancy.Request request)  Unknown
        Nancy.Hosting.Self.dll!Nancy.Hosting.Self.NancyHost.Process(System.Net.HttpListenerContext ctx) Unknown
        Nancy.Hosting.Self.dll!Nancy.Hosting.Self.NancyHost.GotCallback(System.IAsyncResult ar) Unknown
        System.dll!System.Net.LazyAsyncResult.Complete(System.IntPtr userToken) Unknown
        System.dll!System.Net.LazyAsyncResult.ProtectedInvokeCallback(object result, System.IntPtr userToken)   Unknown
        System.dll!System.Net.ListenerAsyncResult.IOCompleted(System.Net.ListenerAsyncResult asyncResult, uint errorCode, uint numBytes)    Unknown
        System.dll!System.Net.ListenerAsyncResult.WaitCallback(uint errorCode, uint numBytes, System.Threading.NativeOverlapped* nativeOverlapped)  Unknown
        mscorlib.dll!System.Threading._IOCompletionCallback.PerformIOCompletionCallback(uint errorCode, uint numBytes, System.Threading.NativeOverlapped* pOVERLAP) Unknown</p>
  </blockquote>
  
  <p>Then followed by a ArgumentNullException:</p>
  
  <p>System.ArgumentNullException occurred
  Message: A first chance exception of type 'System.ArgumentNullException' occurred in System.Web.dll
  Additional information: Value cannot be null.</p>
  
  <blockquote>
    <p>System.Web.dll!System.Web.HttpContextWrapper.HttpContextWrapper(System.Web.HttpContext httpContext) Unknown
        System.Web.Optimization.dll!System.Web.Optimization.Styles.Context.get()    Unknown
        System.Web.Optimization.dll!System.Web.Optimization.Styles.RenderFormat(string tagFormat, string[] paths)   Unknown
        Temp_bfd13a3e77214eca9b1ce8f1a37d244d.dll!RazorOutput.RazorView.Execute()   Unknown
        Nancy.ViewEngines.Razor.dll!Nancy.ViewEngines.Razor.NancyRazorViewBase.ExecuteView(string body, System.Collections.Generic.IDictionary sectionContents)  Unknown
        [Lightweight Function]<br>
        Nancy.ViewEngines.Razor.dll!Nancy.ViewEngines.Razor.RazorViewEngine.RenderView.AnonymousMethod__26(System.IO.Stream stream) Unknown
        Nancy.Hosting.Self.dll!Nancy.Hosting.Self.NancyHost.OutputWithDefaultTransferEncoding(Nancy.Response nancyResponse, System.Net.HttpListenerResponse response)   Unknown
        Nancy.Hosting.Self.dll!Nancy.Hosting.Self.NancyHost.ConvertNancyResponseToResponse(Nancy.Response nancyResponse, System.Net.HttpListenerResponse response)  Unknown
        Nancy.Hosting.Self.dll!Nancy.Hosting.Self.NancyHost.Process(System.Net.HttpListenerContext ctx) Unknown
        Nancy.Hosting.Self.dll!Nancy.Hosting.Self.NancyHost.GotCallback(System.IAsyncResult ar) Unknown
        System.dll!System.Net.LazyAsyncResult.Complete(System.IntPtr userToken) Unknown
        System.dll!System.Net.LazyAsyncResult.ProtectedInvokeCallback(object result, System.IntPtr userToken)   Unknown
        System.dll!System.Net.ListenerAsyncResult.IOCompleted(System.Net.ListenerAsyncResult asyncResult, uint errorCode, uint numBytes)    Unknown
        System.dll!System.Net.ListenerAsyncResult.WaitCallback(uint errorCode, uint numBytes, System.Threading.NativeOverlapped* nativeOverlapped)  Unknown
        mscorlib.dll!System.Threading._IOCompletionCallback.PerformIOCompletionCallback(uint errorCode, uint numBytes, System.Threading.NativeOverlapped* pOVERLAP) Unknown</p>
  </blockquote>
  
  <p>UPDATE: </p>
  
  <ul>
  <li>That last stack trace seems to imply a problem with AspNet.Web.Optimization, perhaps related to this question:</li>
  </ul>
  
  <p><a href="https://stackoverflow.com/questions/23003482/cant-get-system-web-optimization-to-run-with-nancy-self-hosting">Can&#39;t get System.Web.Optimization to run with Nancy Self Hosting</a> </p>