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() != )
{
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(), );
filters.Add(new EwHandleErrorAttribute(), );
}

HandleErrorAttribute 特性使用的更多相关文章

  1. 【转载】MVC使用HandleErrorAttribute自定义异常

    本文导读:在ASP.NET MVC中,可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特 ...

  2. MVC使用HandleErrorAttribute自定义异常

    MVC中,有一个Filter可以捕捉错误,但是它的用法是利用Attribute来实现的,而且只能加在Controller和Action上,所以不能捕捉别出的错误 其实理论上所有的错误肯定产生于Cont ...

  3. 在ASP.NET Core使用Middleware模拟Custom Error Page功能

    一.使用场景 在传统的ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAtt ...

  4. 在MVC中处理异常的总结

    无论是桌面程序还是web程序,异常处理都是必须的. 一般的处理方式是, 捕获异常,然后记录异常的详细信息到文本文件或者数据库中.在Asp.net MVC中可以使用内建的filter——HandleEr ...

  5. ASP.net MVC自定义错误处理页面的方法

    在ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特性,那 ...

  6. ASP.NET MVC 过滤器开发与使用

    ASP.NET MVC 中给我们提供了内置的过滤器,通过过滤器,我们可以在控制器内的方法前后,添加必须的业务逻辑,如权限验证,身份验证,错误处理等. 今天,我们主要介绍3个过滤器:OutputCach ...

  7. ASP.NET MVC编程——错误处理与日记

    ASP.NET MVC的错误处理应考虑到这几个方面:模型绑定期间发生的错误,未能路由到指定操作,针对控制器的错误处理.使用配置文件可以帮助我们处理异常,但是不够灵活和全面:使用HandleErrorA ...

  8. .NET MVC自定义错误处理页面的方法

    在ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特性,那 ...

  9. vue前端开发那些事——后端接口.net core web api

    红花还得绿叶陪衬.vue前端开发离不开数据,这数据正来源于请求web api.为什么采用.net core web api呢?因为考虑到跨平台部署的问题.即使眼下部署到window平台,那以后也可以部 ...

随机推荐

  1. GFS Google File System(中文翻译)

    Google文件系统 GFS是一个可扩展的分布式文件系统,用于大型的.分布式的.对大量数据进行访问的应用.它运行于廉价的普通硬件上,但可以提供容错功能.它可以给大量的用户提供总体性能较高的服务. 1. ...

  2. 【Todo】Python面试题分析记录(修饰器等)

    首先,看这一段代码: class A(object): x = 1 gen = (lambda t: (t for _ in xrange(10)))(x) if __name__ == '__mai ...

  3. tracert 命令详解

    tracert 命令详解 How to Use the TRACERT Utility The TRACERT diagnostic utility determines the route to a ...

  4. 【转载】【Oracle 11gR2】db_install.rsp详解

    [原文]http://blog.csdn.net/jameshadoop/article/details/48086933 ###################################### ...

  5. OpenGL的glTranslatef平移变换函数详解

    OpenGL的glTranslatef平移变换函数详解 glTranslated()和glTranslatef()这两个函数是定义一个平移矩阵,该矩阵与当前矩阵相乘,使后续的图形进行平移变换. 我们先 ...

  6. Spring3 表达式语言(SpEL)介绍

    转载自:http://iyiguo.net/blog/2011/06/19/spring-expression-language/ 下一版本 项目需要使用到SpEL ,做一个保存.

  7. ubuntu server nginx 安装与配置

    ubuntu server nginx 安装与配置 一:关于nginx http://wiki.ubuntu.org.cn/Nginx http://nginx.org/cn http://wiki. ...

  8. VC++编译GSL

    目录 第1章 VC++    1 1.1 修改行结束符    1 1.2 修改#include "*.c" 为 #include "*.inl"    2 1. ...

  9. Unity5.1 新的网络引擎UNET(二) UNET 官方推荐Demo案例

    http://blog.csdn.net/u010019717/article/details/46873153 视频 http://www.iqiyi.com/playlist391685502.h ...

  10. NOI LINUX装机记

    装了差不多一天啊!! 首先自己用虚拟光驱来运行,然后莫名其妙就炸了. 搞到最后刻了一个盘. 然后装完linux之后发现回不到windows7了. 网上找各种资料. 最后搞了个root,再启动的文件中加 ...