【问题标题】:Impossible to show Custom Error page with Nancy on OWIN无法在 OWIN 上显示带有 Nancy 的自定义错误页面
【发布时间】: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 网站时,是否有任何方法可以显示像自定义错误页面这样简单的内容?

【问题讨论】:

    标签: owin nancy


    【解决方案1】:

    你在那里的东西看起来不错,看起来你一直在使用Hosting Nancy with Owin 文档。

    这对我有用:

    Startup.cs(Owin 需要):(我们都对配置函数进行了不同的编码,您只是在使用扩展助手,而我没有。结果相同。这是在我的 App.Web 项目中。 )

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseNancy(options =>
            {
                options.Bootstrapper = new BootStrapper();
                options.PerformPassThrough = context => context.Response.StatusCode == HttpStatusCode.NotFound;
            });
    
            app.UseStageMarker(PipelineStage.MapHandler);
        }
    }
    

    404 处理程序:(根据文档,这在项目中的位置无关紧要,通过实现 IStatusCodeHandler 它将被 Nancy 自动拾取这是在我的 App.WebApi 项目中与其他模块类。)

    public class StatusCode404Handler : IStatusCodeHandler
    {
        public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context)
        {
            return statusCode == HttpStatusCode.NotFound;
        }
    
        public void Handle(HttpStatusCode statusCode, NancyContext context)
        {
            var response = new GenericFileResponse("statuspages/404.html", "text/html")
            {
                StatusCode = statusCode
            };
    
            context.Response = response;
        }
    }
    

    我的 App.Web 项目中的“statuspages”文件夹:

    查看此 SO 帖子以比较使用 GenericFileReponse 或 ViewRenderer (How to display my 404 page in Nancy?)。

    【讨论】:

      猜你喜欢
      • 2019-04-01
      • 2012-12-28
      • 2015-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多