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

All I need is just an error message in text form. But ASP.NET does output an HTML report from each error.

I have a jquery ajax call, and when an error occurs, I get all this shit on the client side.

I created a filter attribute but didn't help.

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 see this

and I would like to see filterContext.Exception.Message located filterContext.Exception.Message

+6
asp.net-mvc error-handling
source share
3 answers

It seems to me that the reason you cannot handle the exception correctly is because it happens outside the MVC pipeline. If you look at the stack trace in the code you sent, there is no link to the System.Web.Mvc code (the start of the exception filter when an exception occurs is called from ControllerActionInvoker.InvokeAction ).

A stack trace indicates that an exception occurs at the end of the ASP.NET pipeline (OnEndRequest) and that it passes through the Autofac component.

To fix this error, you will have to subscribe to the HttpApplication Error event. See the following article on creating a global error handler: http://msdn.microsoft.com/en-us/library/994a1482.aspx . In this case, you can handle the error and redirect to the user error page.

+2
source share

you need to return ContentResult

 ContentResult result = new ContentResult(); result.Content = filterContext.Exception.Message; result.ContentType = MediaTypeNames.Text.Plain; filterContext.Result = result; filterContext.ExceptionHandled = true; 
0
source share

Since you use jQuery and WCF (for the details of your error), you can take a look at this article on how to handle service errors elegantly between jQuery and WCF — you may need to rework your service if you are able to do this.

0
source share

All Articles