本文介绍了如何在南希中显示我的404页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要像这样在Nancy中显示我的404错误页面

I need to display my 404 error page in Nancy like this

if (ErrorCode == 404)
{
  return View["404.html"];
}

该怎么做?

推荐答案

nemesv的答案是正确的,但是我只想添加一个使用ViewRenderer而不是GenericFileResponse的示例.

The answer from nemesv is correct, but I just wanted to add an example using the ViewRenderer instead of the 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;
    }
}

这篇关于如何在南希中显示我的404页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 03:52