如何在ASP.NET MVC 3中创建自定义404错误页面? [英] How to create custom 404 Error pages in ASP.NET MVC 3?

查看:121
本文介绍了如何在ASP.NET MVC 3中创建自定义404错误页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



该解决方案似乎很简单,虽然当我尝试在我的机器上实现这一点时,我遇到以下一行的问题:IController errorsController = new ErrorsController();在Application_Error()函数内。它说找不到类型或命名空间名称ErrorsController(您是否缺少using指令或程序集引用?。



提前感谢您的帮助您可以提供。

解决方案

您应该配置

 < httpErrors> 

部分

 < system.webServer> 



请参阅这篇文章:



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



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



还请求e请注意您的控制器操作中正确的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 unused = prng作为IDisposable;
if(whatever!= null){whatever.Dispose(); }
Response.Status =500 Internal Server Error;
return View();
}

}

配置示例:

 < httpErrors defaultPath =/ error.htmerrorMode =CustomexistingResponse =ReplacedefaultResponseMode =ExecuteURL> 
< remove statusCode =500subStatusCode = - 1/>
< remove statusCode =404subStatusCode = - 1/>
< error statusCode =500path =/ errors / servererror /responseMode =ExecuteURL/>
< error statusCode =404path =/ errors / notfound /responseMode =ExecuteURL/>
< / httpErrors>

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


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.

I did my research before posting this question and came across this link: Custom error pages on 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?".

Thank you in advance for help you may provide.

解决方案

You should configure

<httpErrors>

section under

<system.webServer> 

section group in your web.config file.

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.

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

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.

这篇关于如何在ASP.NET MVC 3中创建自定义404错误页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆