【问题标题】:How can I display a custom errorPage and still return 404如何显示自定义 errorPage 并仍然返回 404
【发布时间】:2012-09-09 22:47:34
【问题描述】:

有一个路由表包含

 routes.MapRoute("404-PageNotFound", "{*url}", new { controller = "Error", action = "PageNotFound" });

web.config 有:

<customErrors mode="RemoteOnly">        
   <error statusCode="404" redirect="/Error/PageNotFound" />
</customErrors>

当没有路由匹配时,ErrorController 会被命中,并且错误视图使用以下方式呈现:

public ActionResult PageNotFound(ViewModelBase model)
 {
     return View(model);
}

响应的状态码是 200,但在这种情况下需要 404。有没有办法返回 http 代码 404 和客户错误页面?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 http-error


    【解决方案1】:
    public ActionResult PageNotFound(ViewModelBase model)
    {
        Response.StatusCode = 404;
        return View(model);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-03-21
      • 1970-01-01
      • 2021-07-17
      • 2022-10-02
      • 2020-08-22
      • 2012-04-24
      • 2014-05-12
      • 2011-10-02
      • 2013-11-23
      相关资源
      最近更新 更多