解决方法
来自nemesv的答案是正确的,但我只想使用ViewRenderer而不是GenericFileResponse添加一个例子.
public class MyStatusHandler : IStatusCodeHandler { private IViewRenderer viewRenderer; public MyStatusHandler(IViewRenderer viewRenderer) { this.viewRenderer = viewRenderer; } public bool HandlesStatusCode(HttpStatusCode statusCode,NancyContext context) { return statusCode == HttpStatusCode.NotFound; } public void Handle(HttpStatusCode statusCode,NancyContext context) { var response = viewRenderer.RenderView(context,"/status/404"); response.StatusCode = statusCode; context.Response = response; } }