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

我是ASP.NET MVC4的新手,正在学习有关该主题的课程.我在错误处理方面有一个非常小的问题.当讲师添加< customErrors模式=“ 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);
        }
    }
}

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

解决方法:

您仍然需要制作一个错误页面.例如,

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>
上一篇:下拉菜单不起作用MVC C#Razor


下一篇:hdu1556 Color the ball 简单线段树