分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

MVC 异常过滤

发布时间:2023-09-06 01:50责任编辑:苏小强关键词:MVC
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 = -1, 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()); ???????} ???}}

MVC 异常过滤

原文地址:https://www.cnblogs.com/chenyishi/p/8876026.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved