Asp.net mvc 如何在ASP.NET MVC 3中创建自定义404错误页?

Asp.net mvc 如何在ASP.NET MVC 3中创建自定义404错误页?,asp.net-mvc,error-handling,http-status-code-404,Asp.net Mvc,Error Handling,Http Status Code 404,在ASP.NET MVC 3中创建自定义错误页的最佳方法是什么?我特别感兴趣的是404错误,还有403和其他错误。我是MVC框架的新手,传统上我来自PHP背景,但学习速度很快 在发布此问题之前,我进行了研究,发现了以下链接: 这个解决方案看起来很简单,但当我试图在我的机器上实现它时,我遇到了以下行的问题:IController errorsController=new errorsController();在Application_Error()函数中。它表示“找不到类型或命名空间名称'Err

在ASP.NET MVC 3中创建自定义错误页的最佳方法是什么?我特别感兴趣的是404错误,还有403和其他错误。我是MVC框架的新手,传统上我来自PHP背景,但学习速度很快

在发布此问题之前,我进行了研究,发现了以下链接:

这个解决方案看起来很简单,但当我试图在我的机器上实现它时,我遇到了以下行的问题:IController errorsController=new errorsController();在Application_Error()函数中。它表示“找不到类型或命名空间名称'ErrorsController'(是否缺少using指令或程序集引用?”

提前感谢您提供的帮助。

您应该配置

<httpErrors>
配置示例:

<httpErrors defaultPath="/error.htm" errorMode="Custom" existingResponse="Replace" defaultResponseMode="ExecuteURL">
      <remove statusCode="500" subStatusCode="-1" />
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="500" path="/errors/servererror/" responseMode="ExecuteURL" />
      <error statusCode="404" path="/errors/notfound/" responseMode="ExecuteURL" />
  </httpErrors>


您可以使用“subStatusCode”属性来控制404.3和其他应用程序。

应用程序的EndRequest
方法添加到
MVCAPApplication
中,以调用“未找到”页面的视图,根据。

可能重复的
public class ErrorsController : Controller
{
    public ActionResult NotFound()
    {
        Response.Status = "404 Not Found";
        return View();
    }

    public ActionResult ServerError()
    {
        byte[] delay = new byte[1];
        RandomNumberGenerator prng = new RNGCryptoServiceProvider();

        prng.GetBytes(delay);
        Thread.Sleep((int)delay[0]);

        IDisposable disposable = prng as IDisposable;
        if (disposable != null) { disposable.Dispose(); }
        Response.Status = "500 Internal Server Error";
        return View();
    }

}
<httpErrors defaultPath="/error.htm" errorMode="Custom" existingResponse="Replace" defaultResponseMode="ExecuteURL">
      <remove statusCode="500" subStatusCode="-1" />
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="500" path="/errors/servererror/" responseMode="ExecuteURL" />
      <error statusCode="404" path="/errors/notfound/" responseMode="ExecuteURL" />
  </httpErrors>