配置Magical Unicorn Mvc错误工具包

时间:2013-02-28 09:32:43

标签: asp.net-mvc asp.net-mvc-4 custom-errors

我正在尝试在我的MVC4网站上配置Magical Unicorn Mvc Error Toolkit(v 2.1.2),但我无法让它工作。这是我的代码:

的Web.config

<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/ServerError">
    <error statusCode="404" redirect="~/Views/Error/NotFound.cshtml" />
</customErrors>

<system.webServer>
   <httpErrors errorMode="Custom" existingResponse="Replace">
     <remove statusCode="404" subStatusCode="-1" />
     <remove statusCode="500" subStatusCode="-1" />
     <error statusCode="404" path="~/Error/NotFound" responseMode="ExecuteURL" />
     <error statusCode="500" path="~/Error/ServerError" responseMode="ExecuteURL" />
   </httpErrors>
<system.webServer>

错误控制器

public class ErrorController : Controller
{
    public ActionResult NotFound()
    {
        Response.StatusCode = (int)HttpStatusCode.NotFound;
        return View();
    }

    public ActionResult ServerError()
    {
        Response.StatusCode = (int)HttpStatusCode.InternalServerError;
        return View();
    }
}

[这些基于此https://stackoverflow.com/a/7499406/236860帖子]

CustomerErrorHandler.cs(App_Start)

using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using WorldDomination.Web.Mvc;
using CustomErrors.App_Start;

[assembly: WebActivator.PreApplicationStartMethod(typeof(CustomErrorHander), "PreStart")]
namespace CustomErrors.App_Start
{
    public static class CustomErrorHander
    {
        public static void PreStart()
        {
            // Register the custom error handling module.
            DynamicModuleUtility.RegisterModule(typeof (CustomErrorHandlingModule));
        }
    }
}

我正在使用IIS Express在Visual Studio 2012中测试此应用程序。如果我尝试导航到不存在的页面,或者转到调用异常的操作方法,我会得到默认的浏览器错误页面或空白页面。

我还修改了ASP.NET MVC Custom Error Pages with Magical Unicorn中建议的上述代码,但这似乎没有任何区别。

任何人都可以指出我正确的方向来实现这一目标。

1 个答案:

答案 0 :(得分:14)

最后,我无法使用Magical Unicorn Mvc Error Toolkit。好消息是我认为不得不这样做!由于我将MVC应用程序部署到IIS 7.5 Web服务器,因此我可以使用Web.config的更高版本的system.webServer.httpErrors部分和自定义错误控制器。

<强>的Web.Config

<system.web>
    <httpRuntime targetFramework="4.5" />
    <compilation debug="false" targetFramework="4.5">
    <customErrors mode="Off" />   <!-- IMPORTANT -->
    ...
</system.web>

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
        <remove statusCode="403" />
        <error statusCode="403" responseMode="ExecuteURL" path="/Error/AccessDenied" />
        <remove statusCode="404" />
        <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />
        <remove statusCode="500" />
        <error statusCode="500" responseMode="ExecuteURL" path="/Error/ApplicationError" />
    </httpErrors>
    ...
</system.webServer>

错误控制器

public class ErrorController : Controller
{
   public ActionResult AccessDenied()
   {
      Response.StatusCode = (int)HttpStatusCode.Forbidden;
      Response.TrySkipIisCustomErrors = true;

      if (Request.IsAjaxRequest())
      {
        // return Json friendly response here
      }

      return View();
   }

   public ActionResult NotFound()
   {
      Response.StatusCode = (int)HttpStatusCode.NotFound;
      Response.TrySkipIisCustomErrors = true;

      if (Request.IsAjaxRequest())
      {
        // return Json friendly response here
      }

      return View();
   }

   public ActionResult ApplicationError()
   {
      Response.StatusCode = (int)HttpStatusCode.InternalServerError;
      Response.TrySkipIisCustomErrors = true;

      if (Request.IsAjaxRequest())
      {
        // return Json friendly response here
      }

      return View();
   }
}
  • 这一切似乎与IIS Express和IIS 7.5配合得很好。
  • Elmah记录错误,而不对默认错误过滤器进行任何更改。
  • Fiddler还建议正确维护正确的HTTP状态代码。