2014-12-09 17 views
2

在當你調用南希:如何模仿的Web API平原NOTFOUND迴應?

return NotFound(); 

的Web API生成下面的響應

HTTP/1.1 404 Not Found 
Cache-Control: no-cache 
Pragma: no-cache 
Expires: -1 
Server: Microsoft-IIS/8.0 
X-AspNet-Version: 4.0.30319 
X-SourceFiles: ... 
X-Powered-By: ASP.NET 
Date: Tue, 09 Dec 2014 12:54:42 GMT 
Content-Length: 0 

在南希,不管我怎麼努力,我不能複製這種行爲,我始終弄404綠色怪物頁面。

我已經實現了一個新的引導程序清除所有處理器和僅添加JsonProcessor,沒有運氣。

public class ApiBootstrapper : DefaultNancyBootstrapper 
{ 
    protected override NancyInternalConfiguration InternalConfiguration 
    { 
     get 
     { 
      return NancyInternalConfiguration.WithOverrides(c => { 
       c.ResponseProcessors.Clear(); 
       c.ResponseProcessors.Add(typeof(JsonProcessor)); 
      }); 
     } 
    } 
} 

我實現IStatusCodeHandler,也沒有運氣

public class NotFoundStatusCodeHandler : IStatusCodeHandler 
{ 
    public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context) 
    { 
     return statusCode == HttpStatusCode.NotFound; 
    } 

    public void Handle(HttpStatusCode statusCode, NancyContext context) 
    { 
     Tuple<string, string>[] headers = { 
      Tuple.Create<string, string>("Cache-Control", "no-cache"), 
      Tuple.Create<string, string>("Pragma", "no-cache"), 
      Tuple.Create<string, string>("Expires", "-1"), 
      Tuple.Create<string, string>("Content-Length", "0") 
     }; 

     context.Response.WithStatusCode(404) 
         .WithHeaders(headers); 
    } 
} 

我會很感激一些幫助。

+0

退房這個答案不夠好:http://stackoverflow.com/a/19326130/2068738 – bonh 2015-11-05 14:14:00

回答

2

根據Martin的回答,我搜索了Nancy響應類型,並發現了Nancy.HeadResponse,它返回一個只響應頭文件的響應,就像Web API一樣。所以,最終的代碼如下:

public class NotFoundStatusCodeHandler : IStatusCodeHandler 
{ 
    public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context) 
    { 
     return statusCode == HttpStatusCode.NotFound; 
    } 

    public void Handle(HttpStatusCode statusCode, NancyContext context) 
    { 
     Tuple<string, string>[] headers = { 
      Tuple.Create<string, string>("Cache-Control", "no-cache"), 
      Tuple.Create<string, string>("Pragma", "no-cache"), 
      Tuple.Create<string, string>("Expires", "-1"), 
      Tuple.Create<string, string>("Content-Length", "0") 
     }; 

     Response response = new Response(); 

     response.WithStatusCode(statusCode) 
       .WithHeaders(headers) 
       .WithContentType(string.Empty); 

     context.Response = new HeadResponse(response); 
    } 
} 

產生爲Web API完全相同的響應。我不得不這樣做

的另一件事是防止IIS發送自己的404錯誤頁面,提交了來回保羅Stovell的博客文章中,馬丁的答覆中提到。

在Web.config文件中添加一行

<system.webServer> 
    ... 
    <httpErrors errorMode="Custom" existingResponse="PassThrough" /> 
    ... 
</system.webServer> 

正如我不斷地學習南希,我可能會拿出一個更好的解決方案。現在,我認爲這是:-)

1

您需要設置內容類型在你的迴應HTML以外的東西,因爲它默認爲

Tuple<string, string>[] headers = { 
      Tuple.Create<string, string>("Cache-Control", "no-cache"), 
      Tuple.Create<string, string>("Pragma", "no-cache"), 
      Tuple.Create<string, string>("Expires", "-1"), 
      Tuple.Create<string, string>("Content-Length", "0") 
     }; 

      context.Response = new TextResponse(JsonConvert.SerializeObject(headers, Formatting.Indented)) 
      { 
       StatusCode = statusCode, 
       ContentType = "application/json" 
      }; 

我也建議你閱讀它描述瞭如何輸出的錯誤,基於這個優秀的博客客戶目前要求什麼。 http://paulstovell.com/blog/consistent-error-handling-with-nancy

+0

感謝線索!你建議的帖子非常好,我會盡快詳細審查。同時,我提出瞭解決方案。 – 2014-12-10 11:23:45

+0

馬丁不幸的是,我不能投你了,因爲我有沒有足夠的聲譽。但是你的回答確實讓我走上了正軌。 TKS – 2014-12-10 12:19:57