Skip to content

Instantly share code, notes, and snippets.

@Mark-Broadhurst
Last active August 29, 2015 14:21
Show Gist options
  • Save Mark-Broadhurst/2397b966daf2e7718c33 to your computer and use it in GitHub Desktop.
Save Mark-Broadhurst/2397b966daf2e7718c33 to your computer and use it in GitHub Desktop.
ASP.NET MVC Json Error Handler Attribute
namespace CII.Mercury.CRM
{
#region Namespaces
using System.Net;
using System.Web.Mvc;
#endregion
public class JsonErrorHandlerAttribute : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
filterContext.HttpContext.Response.StatusCode = 500;
filterContext.ExceptionHandled = true;
filterContext.Result = new JsonResult
{
Data = new
{
success = false,
message = filterContext.Exception.Message,
#if DEBUG
type = filterContext.Exception.GetType().Name,
stacktrace = filterContext.Exception.StackTrace,
source = filterContext.Exception.Source
#endif
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment