Setting up the Magical Unicorn Mvc Error Toolkit

I am trying to configure the Magic Unicorn Mvc Error Toolkit (v 2.1.2) on my MVC4 website, but I cannot get it to work. Here is my code:

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>

Error controller

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

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

[They were based on this https://stackoverflow.com/a/16780/16480/ ]

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));
        }
    }
}

I am testing this application in Visual Studio 2012 using IIS Express. If I try to go to a non-existent page or go to the action method that throws an exception, I will either get the default browser error page or a blank page.

I also modified the above code as suggested on the ASP.NET MVC custom error pages with a magic unicorn , but this does not seem to make any difference.

- , .

+5
1

, Magic Unicorn Mvc Error Toolkit . , , ! MVC - IIS 7.5, system.webServer.httpErrors Web.config .

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-.
+14

All Articles