在當你調用南希:如何模仿的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);
}
}
我會很感激一些幫助。
退房這個答案不夠好:http://stackoverflow.com/a/19326130/2068738 – bonh 2015-11-05 14:14:00