【发布时间】:2016-07-18 05:01:10
【问题描述】:
我有一个使用 OWIN 托管的 Nancy 网站。
在我的 Startup.cs 文件中,我将 PassThroughOptions 定义如下:
public void Configuration(IAppBuilder app)
{
app.UseNancy(o => {
o.PassThroughWhenStatusCodesAre(
HttpStatusCode.NotFound,
HttpStatusCode.InternalServerError
);
o.Bootstrapper = new Bootstrapper();
});
app.UseStageMarker(PipelineStage.MapHandler);
}
我需要传递 NotFound 请求,以便我的站点根目录(robots.txt 或 sitemap.xml)中捆绑的 .less 文件或 miniprofiler-results 或静态文件等内容正常工作。
我还有一个用于 404 代码的自定义 StatusCodeHandler,它还检查自定义标头以区分静态文件(或 .less bundles/miniprofiler)和在我的模块方法中找不到的实际内容。
public void Handle(HttpStatusCode statusCode, NancyContext context)
{
Log.Warn("Not found: " + context.Request.Url);
base.Handle(statusCode, context, "Errors/NotFound");
}
然后这个处理程序实际上应该显示错误页面。
protected void Handle(HttpStatusCode statusCode, NancyContext context, string view)
{
var response = new Negotiator(context)
.WithModel(GetErrorModel(context))
.WithStatusCode(statusCode)
.WithView(view);
context.Response = responseNegotiator.NegotiateResponse(response, context);
}
但永远不会显示错误页面。该请求被处理了 3 次,最终显示默认的 IIS 错误页面(使用 errorMode="Custom" 表示 httpErrors)或只是一个白页(使用 existingResponse="PassThrough" 表示 httpErrors)。
在 OWIN 上托管 Nancy 网站时,是否有任何方法可以显示像自定义错误页面这样简单的内容?
【问题讨论】: