Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Magical Unicorn Mvc Error Toolkit

I am trying to configure the Magical Unicorn Mvc Error Toolkit (v 2.1.2) on my MVC4 web site but I can't get it to work. Here's 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();
    }
}

[These were based on this https://stackoverflow.com/a/7499406/236860 post]

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 navigate to a non-existent page, or go to an action method that calls an exception I either get the default browser error page or a blank page.

I have also modified the above code as suggested at ASP.NET MVC Custom Error Pages with Magical Unicorn but this dis not seem to make any difference.

Can anyone point me in the right direction to get this working.

like image 827
Neilski Avatar asked Feb 28 '13 09:02

Neilski


1 Answers

In the end, I could not get the Magical Unicorn Mvc Error Toolkit to work. The good news is that I don't think I had to! Since I am deploying the MVC application to an IIS 7.5 web server, I could use the later system.webServer.httpErrors section of my Web.config and a custom error controller.

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>

Error Controller

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();
   }
}
  • This all seems to work well with IIS Express and IIS 7.5.
  • Elmah logs the errors without any changes to the default error filters.
  • Fiddler also suggests that the correct HTTP Status codes are also being correctly maintained.
like image 146
Neilski Avatar answered Sep 30 '22 09:09

Neilski