Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C#
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 DataMisalignedException();
            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
C#
namespace TestExceptionHandling
{
    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
    }
}
Posted
Updated 20-Aug-17 16:53pm
v4
Comments
Graeme_Grant 20-Aug-17 19:39pm    
Your code was extremely difficult to read. Always be respectful and format correctly if you want others to respond.
Dave Kreskowiak 20-Aug-17 19:58pm    
This usually happens when your error handling code/controller/view also throws an exception.

1 solution

I found the solution. When I was creating the views for the various specific exception types, I was specifying that it should use _Layout.cshtml for layout. I discovered that that causes an exception. So now I am not checking the "Use a Layout Page" box. My [HandleError] functionality is working.

Dave, you were right; I just needed to find out where such an exception might come from. Thanks.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900