MVC 异常过滤
2024-10-21 12:49:50
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using cpf360.Common;
using cpf360.DTO; namespace hanli_project.Filters
{
public class HanliExceptionAttribute : IExceptionFilter
{
void IExceptionFilter.OnException(ExceptionContext filterContext)
{
Exception exception = filterContext.Exception;
if (filterContext.ExceptionHandled)
{
return;
} //返回数据
filterContext.Result = new JsonResult() { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = new OutputData() { data = null, code = -, message = "服务器繁忙" } }; //记录日志
Log4netHelper.LogError(exception); // 设置自定义异常已经处理,避免其他过滤器异常覆盖
filterContext.ExceptionHandled = true; // 在派生类重写时,设置或者重写一个值该值指定是否禁用ISS7.0中自定义错误
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; }
}
}
using System.Web;
using System.Web.Mvc;
using hanli_project.Filters; namespace hanli_project
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
//注册异常过滤器
filters.Add(new HanliExceptionAttribute());
filters.Add(new HandleErrorAttribute());
}
}
}
最新文章
- JavaScript的写类方式(6)
- Replication的犄角旮旯(五)--关于复制identity列
- 数位DP HDU3652
- 滚动光效shader
- 让PHP 5.3支持MSSQL连接
- 百度富文本编辑器UEditor安装配置全过程
- PHP CURL访问HTTPS使用详解
- 什么是PHP Guzzle?
- Wide character in print at hcp.pl line 21.
- MVC 使用EF Code First数据迁移之添加字段
- 11.hibernate的连接查询
- Hibernate锁机制
- js判断空值
- Angular为什么选择TypeScript?
- json文件常用代码
- debug错误
- Emacs 设置C++代码风格
- 视图控制器生命周期中各个重要的方法(Swift) (Important Methods during the Lifecycle of a View Controller)
- zookeeper 安装的三种模式
- 解决Mac下npm权限问题