【问题标题】:Configuring Magical Unicorn Mvc Error Toolkit配置 Magical Unicorn Mvc 错误工具包
【发布时间】:2013-02-14 10:25:16
【问题描述】:

我正在尝试在我的 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 的建议修改了上面的代码,但这似乎没有任何区别。

任何人都可以指出正确的方向以使其正常工作。

【问题讨论】:

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


    【解决方案1】:

    最后,我无法让 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 状态代码。

    【讨论】:

    猜你喜欢
    • 2021-07-18
    • 2021-07-11
    • 2017-11-16
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多