How to display a generic error page in Asp.Net MVC 2

Posted by Picflight on Stack Overflow See other posts from Stack Overflow or by Picflight
Published on 2010-06-17T01:09:06Z Indexed on 2010/06/17 1:12 UTC
Read the original article Hit count: 291

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?

© Stack Overflow or respective owner

Related posts about asp.net-mvc-2

Related posts about error-handling