如何在 ASP.NET MVC4 上打开 customErrors 模式?

How can I turn customErrors mode on ASP.NET MVC4?

我是 ASP.NET MVC4 的新手,正在学习有关该主题的课程。我在错误处理方面有一个非常小的问题。当讲师将 <customErrors mode = "On"/> 添加到 web.config 文件中的 <system.web> 标记时,他将被重定向到友好的错误页面(而不是堆栈跟踪)。

当我进行此更改时,我仍然被定向到堆栈跟踪 "yellow page of death"。

由于我正在齐心协力尽可能彻底地理解这一点,所以我想在这里问一下。为什么转向 <customErrors mode="On"/> 对教练有效,对我无效?

这是我的 <system.web> 标签:

<system.web>

    <customErrors mode="On"/>

    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>

</system.web>

这是我的 Global.asax.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace OdeToFood
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }
    }
}

最后,我的 FilterConfig.cs

using System.Web;
using System.Web.Mvc;

namespace OdeToFood
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
    }
}

我目前有默认视图 Error.cshtml。

我也试过了

<customErrors mode="On" defaultRedirect="Error"/>

<customErrors mode="On" defaultRedirect="~/Error.cshtml"/>

我正在使用这段代码来强制错误:

    public class CuisineController : Controller
    {
        //
        // GET: /Cuisine/
        public ActionResult Search(string name = "french")
        {

            throw new Exception("Something terrible has happened");

            var message = Server.HtmlEncode(name);

            return Content(message);
        }
    }
}

如您所知,我对此很陌生,如果这是一个无聊的问题,我深表歉意,但我正在尽我所能学习。

您还需要制作一个错误页面。例如,

Web.config

<system.web>
  ...
  <customErrors defaultRedirect="~/Common/Error" mode="On"/>
</system.web>

通用控制器

public class CommonController : Controller
{
    // Error
    public ActionResult Error()
    {
        this.Response.StatusCode = 503;
        this.Response.TrySkipIisCustomErrors = true;

        return View();
    }
}

~/Views/Shared/Error.cshtml

@{
    ViewBag.Title = "Error";
}
<h2>@ViewBag.Title</h2>
<p>
    Error ...
</p>