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

时间:2022-10-09 21:03:19

what is the best way to create custom error pages in ASP.NET MVC 3? The one I am particularly interested in is a 404 error, but also 403, and others. I am new to the MVC framework, traditionally I come from a PHP background, but am learning quickly.

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

I did my research before posting this question and came across this link: Custom error pages on asp.net MVC3

我在发布此问题之前做了我的研究,并发现了这个链接:asp.net MVC3上的自定义错误页面

That solution seems simple although when I try to implement that on my machine, I get a problem with the following line: IController errorsController = new ErrorsController(); inside the Application_Error() function. It says "The type or namespace name 'ErrorsController' could not be found (are you missing a using directive or an assembly reference?".

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

Thank you in advance for help you may provide.

提前感谢您提供的帮助。

2 个解决方案

#1


15  

You should configure

你应该配置

<httpErrors>

section under

<system.webServer> 

section group in your web.config file.

web.config文件中的节组。

Please refer to this article:

请参考这篇文章:

http://www.iis.net/ConfigReference/system.webServer/httpErrors

Additionally you can use the error controller which you linked in your question, however initial flow should be managed by IIS. By this section you can instruct IIS that it should execute urls which are managed by your controller.

此外,您可以使用您在问题中链接的错误控制器,但初始流程应由IIS管理。通过本节,您可以指示IIS应该执行由您的控制器管理的URL。

Also please take care about proper Response.Status property string in your controller's actions as proposed solution returns "200 OK" which may be confusing for browsers. For example

另外请注意控制器操作中正确的Response.Status属性字符串,因为建议的解决方案返回“200 OK”,这可能会让浏览器感到困惑。例如

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();
    }

}

Configuration example:

<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>

You can control 404.3 and other using "subStatusCode" attribute.

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

#2


2  

Add an Application_EndRequest method to your MvcApplication to call a view for your "not found" page, per Marco's Better-Than-Unicorns MVC 404 Answer.

根据Marco的Better-Than-Unicorns MVC 404 Answer,为您的MvcApplication添加一个Application_EndRequest方法,为您的“未找到”页面调用视图。

#1


15  

You should configure

你应该配置

<httpErrors>

section under

<system.webServer> 

section group in your web.config file.

web.config文件中的节组。

Please refer to this article:

请参考这篇文章:

http://www.iis.net/ConfigReference/system.webServer/httpErrors

Additionally you can use the error controller which you linked in your question, however initial flow should be managed by IIS. By this section you can instruct IIS that it should execute urls which are managed by your controller.

此外,您可以使用您在问题中链接的错误控制器,但初始流程应由IIS管理。通过本节,您可以指示IIS应该执行由您的控制器管理的URL。

Also please take care about proper Response.Status property string in your controller's actions as proposed solution returns "200 OK" which may be confusing for browsers. For example

另外请注意控制器操作中正确的Response.Status属性字符串,因为建议的解决方案返回“200 OK”,这可能会让浏览器感到困惑。例如

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();
    }

}

Configuration example:

<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>

You can control 404.3 and other using "subStatusCode" attribute.

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

#2


2  

Add an Application_EndRequest method to your MvcApplication to call a view for your "not found" page, per Marco's Better-Than-Unicorns MVC 404 Answer.

根据Marco的Better-Than-Unicorns MVC 404 Answer,为您的MvcApplication添加一个Application_EndRequest方法,为您的“未找到”页面调用视图。