Asp.net mvc 配置神奇独角兽Mvc错误工具包

Asp.net mvc 配置神奇独角兽Mvc错误工具包,asp.net-mvc,asp.net-mvc-4,custom-errors,Asp.net Mvc,Asp.net Mvc 4,Custom Errors,我试图在我的MVC4网站上配置神奇的独角兽Mvc错误工具包(v2.1.2),但我无法让它工作。这是我的密码: Web.config <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/ServerError"> <error statusCode="404" redirect="~/Views/Error/NotFound.cshtml" /> </c

我试图在我的MVC4网站上配置神奇的独角兽Mvc错误工具包(v2.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>
<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>
[这些都是基于这篇文章]

CustomerErrorHandler.cs(应用程序启动)

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

我也修改了上面的代码,但这似乎没有任何区别


有人能给我指出正确的方向让它工作吗。

最后,我无法让神奇的独角兽Mvc错误工具包工作。好消息是我不认为我必须这么做!由于我正在将MVC应用程序部署到IIS 7.5 web服务器,因此我可以使用web.config中更高版本的system.webServer.httpErrors部分和自定义错误控制器

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>
<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>
  • 这一切似乎都适用于IIS Express和IIS 7.5
  • Elmah记录错误,而不对默认错误过滤器进行任何更改
  • Fiddler还建议正确的HTTP状态代码也得到了正确的维护

在浪费时间回答问题之后,结果证明这是一个非常好的方法,非常简单。谢谢你,Michael,很高兴它能帮上忙。
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();
   }
}