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

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

问题描述

什么是创建ASP.NET MVC 3自定义错误页的最佳途径?所述一个我特别感兴趣的是一个404错误,而且还403,和其他。我是新来的MVC框架,传统我来自一个PHP的背景,但我很快学会。

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 MVC3 自定义错误页

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

该解决方案看起来很简单,虽然当我试图实现我的机器上,我得到以下行的一个问题:一个IController errorsController =新ErrorsController();里面的Application_Error()函数。它说:类型或命名空间名称'ErrorsController'找不到(是否缺少using指令或程序集引用?

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.

推荐答案

您应该配置

<httpErrors>

<system.webServer> 

节组在你的web.config文件。

section group in your web.config file.

请参考这篇文章:

<一个href=\"http://www.iis.net/ConfigReference/system.webServer/httpErrors\">http://www.iis.net/ConfigReference/system.webServer/httpErrors

此外,你可以使用你在你的问题联系在一起的差错控制,但是最初的流动应该由IIS进行管理。通过这一部分,您可以指示IIS,它应该执行这是由你的控制器管理的网址。

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.

也请照顾一下贵控制器的行动是提出的解决方案返回200 OK,这可能会产生混淆的浏览器正确Response.Status属性字符串。例如:

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

}

配置例如:

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

您可以使用子状态code属性控制404.3等。

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

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

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