views:

573

answers:

2

I have the following in my base controller:

    protected override void OnException(ExceptionContext filterContext)
    {
        if (filterContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }

        // If custom errors are disabled, we need to let the normal ASP.NET exception handler
        // execute so that the user can see useful debugging information.
        if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
        {
            return;
        }

        Exception exception = filterContext.Exception;

        // If this is not an HTTP 500 (for example, if somebody throws an HTTP 404 from an action method),
        // ignore it.
        if (new HttpException(null, exception).GetHttpCode() != 500)
        {
            return;
        }
        // TODO: What is the namespace for ExceptionType?
        //if (!ExceptionType.IsInstanceOfType(exception))
        //{
        //    return;
        //}

        // Send Email
        MailException(exception);

        // TODO: What does this line do?
        base.OnException(filterContext);

        filterContext.Result = new ViewResult
        {
            ViewName = "Error"
        };
        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.StatusCode = 500;
    }

In my Shared folder, I have an Error.aspx View.

Web.config

<customErrors mode="On" />

I am still seeing the yellow screen when an exception occurs. What am I doing incorrectly?

A: 

I would imagine that invoking base.OnException handler is what is causing your problem. Without actually looking at the code, I would imagine that it is what is responsible for handling the error and generating a response with the exception and stack trace. Remove that line from your code -- it's not needed as since you're replacing the ViewResult anyway.

I would recommend that you use ELMAH and implement a HandleError attribute that works with it: see this question. ELMAH is very flexible and configuration driven, rather than code driven.

tvanfosson
I have tried commenting out the base.OnException line as well and I get the same yellow page.
Picflight
@Picflight - time to fire up the debugger and see what's going on. It's possible that it's taking one of the early returns out of your code. It's also possible that your code is, itself, causing an exception -- for example, we have no idea what happens inside `MailException`. I'd put a breakpoint at the top of the method and step through it to see what is happening.
tvanfosson
A: 
Server.ClearError()

What happens if you call that?

Matthew Abbott