在MVC中返回带有404状态代码的自定义404页面?

Inx*_*x51 9 c# asp.net-mvc custom-error-pages http-status-code-404

我正在尝试使用自定义错误页面返回404状态代码.但是我继续得到200,因为只要浏览器和服务器可以告诉页面确实存在.

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

<customErrors mode="On" defaultRedirect="~/404" >
    <error statusCode="404" redirect="~/404"/>
</customErrors>
Run Code Online (Sandbox Code Playgroud)

其中404是指向"未找到页面"控制器和操作的路径.

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

public ActionResult PageNotFound(string locale)
{
    Response.StatusCode = 404;
    return View();
}
Run Code Online (Sandbox Code Playgroud)

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

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

Mak*_*Vi. 9

我有同样的问题,解决方案发布在这里:

public ActionResult NotFound()
{
    Response.StatusCode = 404; 
    Response.TrySkipIisCustomErrors = true; <---
    return View();
}
Run Code Online (Sandbox Code Playgroud)