【MVC__ExceptionFilter】全局异常处理
2021-03-14 08:35
标签:通过 ror global and head TBase redirect sharp ilog 【MVC__ExceptionFilter】全局异常处理 标签:通过 ror global and head TBase redirect sharp ilog 原文地址:https://www.cnblogs.com/kikyoqiang/p/12812188.html通过实现接口
IExceptionFilter
Global文件添加
GlobalFilters.Filters.Add(new ExExceptionFilter());
public class ExExceptionFilter : IExceptionFilter
{
private static ILog log = LogManager.GetLogger(typeof(ExExceptionFilter));
public void OnException(ExceptionContext filterContext)
{
log.Error("出现未处理异常", filterContext.Exception);
HttpRequestBase request = filterContext.RequestContext.HttpContext.Request;
if (request.Headers.AllKeys.Contains("X-Requested-With") && request.Headers["X-Requested-With"].Equals("XMLHttpRequest"))
{
AjaxResult ajaxResult = new AjaxResult(0, "发生错误");
ajaxResult.data.Add("Exception", filterContext.Exception.Message);
ajaxResult.data.Add("detail", filterContext.Exception.ToString());
filterContext.Result = new JsonResult() { Data = ajaxResult };
}
else
{
filterContext.Result = new RedirectResult("/Manager/Manager/Error");
}
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
//filterContext.HttpContext.Response.StatusCode = 500;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
}
文章标题:【MVC__ExceptionFilter】全局异常处理
文章链接:http://soscw.com/essay/64495.html