【问题标题】:Returning a custom 404 page with 404 statuscode in MVC?在 MVC 中返回带有 404 状态码的自定义 404 页面?
【发布时间】:2014-07-29 19:52:31
【问题描述】:

我正在尝试使用我的自定义错误页面返回 404 状态代码。 然而,我不断得到 200 的回报,因为就浏览器和服务器所知,该页面确实存在。

到目前为止我一直在尝试的是:

<customErrors mode="On" defaultRedirect="~/404" >
    <error statusCode="404" redirect="~/404"/>
</customErrors>

其中 404 是到“找不到页面”控制器和操作的路由。

我也尝试在我的操作中设置状态码..

public ActionResult PageNotFound(string locale)
{
    Response.StatusCode = 404;
    return View();
}

但这最终只会显示默认的服务器错误页面(带有红色错误消息文本的灰色页面)

关于如何解决这个问题的任何想法?

【问题讨论】:

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


【解决方案1】:

我遇到了同样的问题,解决方法贴在here:

public ActionResult NotFound()
{
    Response.StatusCode = 404; 
    Response.TrySkipIisCustomErrors = true; <---
    return View();
}

【讨论】:

    【解决方案2】:

    你试过了吗:

    return HttpNotFound();
    

    您可以在此处找到更多信息:

    https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.httpnotfound%28v=vs.118%29.aspx

    【讨论】:

      猜你喜欢
      • 2018-04-05
      • 1970-01-01
      • 2013-03-01
      • 2011-11-22
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 2011-05-17
      • 1970-01-01
      相关资源
      最近更新 更多