Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of that HTML error report in ASP.NET MVC?

All I need would be just the error message in plain text. But ASP.NET is doing some HTML report output from every error.

I have a jquery ajax call and when an error is thrown I'm getting all that crap over to the client side.

I've created a filter attribute but didn't helped.

public class ClientErrorHandler : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        var responce = filterContext.RequestContext.HttpContext.Response;
        responce.Write(filterContext.Exception.Message);
        responce.ContentType = MediaTypeNames.Text.Plain;
        filterContext.ExceptionHandled = true;
    }
}

EDIT

I'm seeing this

and I'd like to see just what is in here filterContext.Exception.Message

like image 640
user137348 Avatar asked Aug 26 '10 12:08

user137348


1 Answers

It looks to me like the reason why you cannot correctly handle the exception is because it happens outside of the MVC pipeline. If you look at the stack trace in the code you posted there is no reference to System.Web.Mvc code (the firing of exception filters when an exception occurs is called from ControllerActionInvoker.InvokeAction).

The stack trace indicates that the exception happens late in the ASP.NET pipeline (OnEndRequest) and that it's coming through the Autofac component.

To capture this error you would have to subscribe to the HttpApplication's Error event. See the following article on creating a global error handler: http://msdn.microsoft.com/en-us/library/994a1482.aspx . In this event you can handle the error and redirect to a custom error page.

like image 175
marcind Avatar answered Sep 30 '22 21:09

marcind