从 nancy bootstrapper 渲染视图?
Render view from nancy bootstrapper?
是否可以在 NancyModule 之外呈现视图?例如,我希望能够像这样在错误管道中呈现视图:
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
}
});
}
}
通过从容器中提取 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 是否会破坏任何东西,但它似乎有效。
是否可以在 NancyModule 之外呈现视图?例如,我希望能够像这样在错误管道中呈现视图:
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
}
});
}
}
通过从容器中提取 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 是否会破坏任何东西,但它似乎有效。