VisualBean
2/22/2017 - 7:50 PM

GlobalErrorAttribute.cs

public class GlobalErrorHandler : HandleErrorAttribute
{
    private bool IsAjax( ExceptionContext filterContext )
    {
        return filterContext.HttpContext.Request.Headers[ "X-Requested-With" ] == "XMLHttpRequest";
    }
    public override void OnException( ExceptionContext filterContext )
    {
        if ( filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled )
        {
          return;
        }

        if ( IsAjax( filterContext ) )
        {
          filterContext.HttpContext.Response.Clear();
          filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
          filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
          if (filterContext.Exception is ErrorToast)
          {
              var ex = filterContext.Exception as ErrorToast;
              var currentUser = HttpContext.Current.User.Identity.Name;
              MessageEmitter.ShowToast(currentUser.Username, ex.Title, ex.Message, ex.Type);                 
          }
        }
        filterContext.ExceptionHandled = true;
        ...
    }
}