【问题标题】:ASP.NET MVC redirect to 403/404 errorASP.NET MVC 重定向到 403/404 错误
【发布时间】:2018-05-02 11:39:34
【问题描述】:

我有以下 web.config 部分:

<customErrors mode="On" defaultRedirect="/Home/Error">
  <error statusCode="404" redirect="/Home/Error404"/>
  <error statusCode="403" redirect="/Home/Error403"/>
  <error statusCode="500" redirect="/Home/Error"/>
</customErrors>

然后我有以下代码:

return HttpNotFound();

当我调用此代码时,我得到以下页面:

为什么我看不到我的特殊页面?

【问题讨论】:

  • 您是否尝试过将其托管在测试环境中
  • 是的,我在 localhost 上测试

标签: asp.net-mvc http-status-code-404 custom-error-pages custom-error-handling


【解决方案1】:

请确保以下几点

1) 所有自定义错误操作都有自己的操作方法,如下所示。

public class ErrorController : Controller
{
    public ViewResult Index()
    {
        return View("Error");
    }
    public ViewResult Error404()
    {
        Response.StatusCode = 404;  //you may want to set this to 200
        return View("NotFound");
    }
    public ViewResult Error403()
    {
        ------
    }
}

2) 并确认所有有自己观点的动作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-14
    • 2015-08-22
    • 1970-01-01
    • 2013-06-18
    • 2020-05-30
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    相关资源
    最近更新 更多