Skip to content

Instantly share code, notes, and snippets.

@martinnormark
Created January 23, 2013 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinnormark/4603530 to your computer and use it in GitHub Desktop.
Save martinnormark/4603530 to your computer and use it in GitHub Desktop.
Log handled exception in AJAX request to ELMAH
using System.Net;
using System.Web.Mvc;
namespace MyApp.Web.PresentationLogic.ActionFilters
{
public class HandleAjaxErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
if (!filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
{
base.OnException(filterContext);
return;
}
else
{
if (filterContext.HttpContext.IsDebuggingEnabled)
{
return;
}
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
filterContext.HttpContext.Response.StatusDescription = filterContext.Exception.Message;
Elmah.ErrorSignal.FromCurrentContext().Raise(filterContext.Exception);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment