2017-01-18 4 views
2

NancyModuleの外部でビューをレンダリングすることはできますか?例えば、私はそうのようなエラーパイプラインでのビューをレンダリングすることができるようにしたい:nancy bootstrapperからのレンダービュー?

public class MyBootstrapper: DefaultNancyBootstrapper 
{ 

    protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context) 
    { 
     pipelines.OnError += ((ctx, e) => 
     { 

      if (context.Request.Headers.Accept.Any(c => c.Item1.Equals("text/html"))) 
      { 
       // Render view using e here 
      } 

     }); 
    } 
} 

答えて

1

がコンテナからするViewFactoryを抽出し、それを解決:

public class MyBootstrapper: DefaultNancyBootstrapper 
{ 

    protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context) 
    { 
     pipelines.OnError += ((ctx, e) => 
     { 

      if (context.Request.Headers.Accept.Any(c => c.Item1.Equals("text/html"))) 
      { 
       IViewFactory viewFactory = container.Resolve<IViewFactory>(); 
       return viewFactory.RenderView("Error", new {Message = ex.Message}, new ViewLocationContext() { Context = context, ModuleName = "", ModulePath = "" }); 
      } 

     }); 
    } 
} 

ない考えであれば、空白のModuleName & ModulePathは何かを破壊しますが、うまくいくようです。

関連する問題