2016-06-17 152 views
0

我有一個網站使用南希使用OWIN託管。無法在OWIN上顯示Nancy的自定義錯誤頁面

在我Startup.cs文件I定義PassThroughOptions如下:

public void Configuration(IAppBuilder app) 
{ 
    app.UseNancy(o => { 
     o.PassThroughWhenStatusCodesAre(
      HttpStatusCode.NotFound, 
      HttpStatusCode.InternalServerError 
      ); 
     o.Bootstrapper = new Bootstrapper(); 
    }); 

    app.UseStageMarker(PipelineStage.MapHandler); 
} 

我需要直通的NOTFOUND要求,使物像我捆綁.LESS文件或miniprofiler,結果或靜態文件在我的網站的根目錄(robots.txt或sitemap.xml)中工作。

我也有一個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); 
} 

但是錯誤頁面從不顯示。該請求被處理了三次,最終顯示了默認的IIS錯誤頁面(對於httpErrors使用errorMode =「Custom」)或簡單的白頁(對於httpErrors使用existingResponse =「PassThrough」)。

當在OWIN上託管南希網站時,是否有任何方式顯示如自定義錯誤頁面這樣簡單的內容?

回答

0

你有什麼看起來不錯,它看起來像你一直在使用Hosting Nancy with Owin文檔。

這裏對我來說是什麼在起作用:

(需要Owin)的Startup.cs:(我們倆都不同編碼的配置功能,你只是使用擴展幫助,而我不一樣。 。結果這是我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它會被自動拾取南希這是在我的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' 文件夾:

Visual Studio folder structure

檢查這個SO張貼使用GenericFileReponse或視圖解析器(How to display my 404 page in Nancy?)的比較。

相關問題