I have a sizeable MVC5 application in VS2015 that accesses around 25 tables in the database. I'm nearly finished with it and am trying to set up exception handling using HandleException.
Unfortunately, I didn't realize that if you experiment with it, moving throw statements and HandleError attributes around, turning the customErrors element off and on, and removing (remarking) and re-enabling RegisterGlobalFilters, the program gets into a state where no matter what you do, you can never get the exception handling with HandleError working again. I am hoping I am wrong and have not permanently disabled HandleError attributes in my application. It is ignoring my HandleError attributes and displaying unhandled error pages.
Aside from my large production app, I have two small test apps, one in which the HandleError attribute is working perfectly and another which has also similarly gotten its HandleError license revoked (figure of speech).
Below is code from my small app that is failing and showing unhandled errors for the ArithmeticException and the DataException errors. Any ideas what could be wrong?
What I have tried:
Web.config
<system.web>
<customErrors mode="On">
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data;
namespace TestExceptionHandling.Controllers
{
[HandleError(ExceptionType = typeof(ArithmeticException), View = "ArithmeticException")]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HandleError(ExceptionType = typeof(DataMisalignedException), View = "DataMisaligned")]
public ActionResult About()
{
throw new ArithmeticException("Oops, an arithmetic error occurred.");
ViewBag.Message = "Your application description page.";
return View();
}
[HandleError(ExceptionType = typeof(DataException), View = "DataException")]
public ActionResult Contact()
{
throw new DataException("Oops, a data exception error occurred.");
ViewBag.Message = "Your contact page.";
return View();
}
}
}
ArithmeticException.cshtml
@{
ViewBag.Title = "ErrorView01";
Layout = "~/Views/Shared/_LoginPartial.cshtml";
}
<h4>ErrorView01<h4>
<h4>An arithmetic exception occurred.</h4>
DataMisaligned.cshtml
@{
ViewBag.Title = "DataMisaligned";
Layout = "~/Views/Shared/_LoginPartial.cshtml";
}
DataMisaligned.cshtml
<h2>Your action could not be completed due to a data misalignment issue.</h2>
DataException.cshtml
{
ViewBag.Title = "DataException";
Layout = "~/Views/Shared/_LoginPartial.cshtml";
}
DataException.cshtml
<h2>The action could not be completed due to a Data Exception. If deleting a data object, e'g' a DOT, make sure all subordinate objects, .e.g. notices, letters, etc. are individually deleted first. This is required to maintain referential integrity and avoid orphaned database objects.</h2>
All of the above views are under ~/Views/Shared
FilterConfig.cs
namespace TestExceptionHandling
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
}