HandleErrorAttribute 特性使用
2015-07-14 17:59
267 查看
public class EwHandleErrorAttribute : HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { if (ICTConfiguration.Debug) { base.OnException(filterContext); return; } if (filterContext.ExceptionHandled) { return; } if (filterContext.HttpContext.Response.IsRequestBeingRedirected) { return; } var httpCode = new HttpException(null, filterContext.Exception).GetHttpCode(); if (!ExceptionType.IsInstanceOfType(filterContext.Exception)) { return; } if (new HttpException(null, filterContext.Exception).GetHttpCode() != 500) { return; } ExceptionHelper.LogException(filterContext.Exception, HttpContext.Current); bool isAjaxCall = string.Equals("XMLHttpRequest", filterContext.HttpContext.Request.Headers["x-requested-with"], StringComparison.OrdinalIgnoreCase); if (isAjaxCall) { string message = filterContext.Exception.Message; if (filterContext.Exception is HttpRequestValidationException) { message = "包含非法字符"; } filterContext.Result = new JsonResult() { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = new { succeed = false, ret = httpCode, msg = message } }; } else { var controllerName = (string)filterContext.RouteData.Values["controller"]; var actionName = (string)filterContext.RouteData.Values["action"]; var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName); filterContext.Result = new ViewResult() { ViewName = View, MasterName = Master, ViewData = new ViewDataDictionary(model), TempData = filterContext.Controller.TempData }; filterContext.HttpContext.Response.Redirect("/500.html"); } filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.Clear(); filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; filterContext.HttpContext.Server.ClearError(); } }
global注册过滤器
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new JoinEnterpriseAttribute(), 1); filters.Add(new EwHandleErrorAttribute(), 2); }
相关文章推荐
- [转] 编写高效的 CSS 选择器
- C++编译器与链接器工作原理
- hdu 1052 Tian Ji -- The Horse Racing
- Spring分布式事务实现
- 把请求数据映射成model类
- sql中的Tran和C#中的TransactionScope介绍
- nagios监控iptables状态
- 路由转发原则
- OpenStack Keystone配置产生错误Access denied for user 'keystone'@'10.0.2.15' (using password: YES)") None N
- TeamViewer远程唤醒主机实战教程(多图)
- 新工作要做的事宜
- oracle 备份数据库对象(存储过程PROCEDURE,FUNCTION,VIEW,TRIGGER...)
- Stack集合、queue集合、hashtable集合
- Android开发:shape和selector和layer-list的(详细说明)
- Drools入门-----------环境搭建,分析Helloworld Drools5.0的xls文件转drl文件提升解析效率 使用BRMS的Tomcat6.0配置
- cocos2d-x 3.6 程序流程
- freeswitch与E1设备对接方法
- UVA 11082 Matrix Decompressing 矩阵解压(最大流,经典)
- 对Java并发编程的几点思考
- ASP.NET DropDownList控件回传值无刷新操作。