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.