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

时间:2015-02-05 19:02:51

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor

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

当我做出这个改变时,我仍然被引导到堆栈跟踪“死亡黄页”。

由于我正在努力尽可能彻底地理解这一点,我想我会在这里问。为什么转动<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);
        }
    }
}

正如你所知,我对此非常陌生,所以如果这是一个无聊的问题我很抱歉,但我正尽力学习。

1 个答案:

答案 0 :(得分:4)

您仍需要制作错误页面。例如,

的Web.config

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

CommonController

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

        return View();
    }
}

〜/查看/共享/ Error.cshtml

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