.Net Core中使用ExceptionFilter过滤器的方法
2020-03-11 16:58
417 查看
.Net Core中有各种Filter,分别是AuthorizationFilter、ResourceFilter、ExceptionFilter、ActionFilter、ResultFilter。可以把他们看作是.Net Core自带的AOP的扩展封装。
今天来看其中的一种:ExceptionFilter(用于全局的异常处理)
首先新建一个.Net Core MVC的项目
新建一个控制器:
这里我们可以看到代码运行到16行时会报一个索引项超出集合范围的错误
按照常规的思维我们在代码中会加异常处理,如下:
try { var range = Enumerable.Range(1, 3).ToArray(); var result = range[4]; return View(); } catch (Exception ex) { throw new Exception(ex.Message); }
但是每个方法都这样加会不会觉得很烦?有没有想过只写一次就可以了
上代码:
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ViewFeatures; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace ExceptionFilter.Filter { public class CustomerExceptionFilter : Attribute, IExceptionFilter { private readonly IHostingEnvironment _hostingEnvironment; private readonly IModelMetadataProvider _modelMetadataProvider; public CustomerExceptionFilter(IHostingEnvironment hostingEnvironment, IModelMetadataProvider modelMetadataProvider) { _hostingEnvironment = hostingEnvironment; _modelMetadataProvider = modelMetadataProvider; } /// <summary> /// 发生异常进入 /// </summary> /// <param name="context"></param> public async void OnException(ExceptionContext context) { if (!context.ExceptionHandled)//如果异常没有处理 { if (_hostingEnvironment.IsDevelopment())//如果是开发环境 { var result = new ViewResult { ViewName = "../Handle/Index" }; result.ViewData = new ViewDataDictionary(_modelMetadataProvider, context.ModelState); result.ViewData.Add("Exception", context.Exception);//传递数据 context.Result = result; } else { context.Result = new JsonResult(new { Result = false, Code = 500, Message = context.Exception.Message }); } context.ExceptionHandled = true;//异常已处理 } } } }
我们在方法中先以特性来使用,加上这句代码:
[TypeFilter(typeof(CustomerExceptionFilter))]
之后会跳到这个视图:../Handle/Index ,会将异常信息传入到此视图
视图页代码:
<p>Message:@ViewData["Exception"]</p>
(可以自行封装。。。)
我们还可以定义成全局的
在Startup类中的ConfigureServices方法中加入这句代码
services.AddControllersWithViews(option => { option.Filters.Add<CustomerExceptionFilter>(); }); //3.0以下的版本好像应该这样写:services.AddMvc();
到此这篇关于.Net Core中使用ExceptionFilter过滤器的方法的文章就介绍到这了,更多相关.Net Core使用ExceptionFilter内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
相关文章推荐
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
- 一张图搞定OAuth2.0 在Office应用中打开WPF窗体并且让子窗体显示在Office应用上 彻底关闭Excle进程的几个方法 (七)Net Core项目使用Controller之二
- .net core 2使用ef core 2.0以db first方法创建实体类
- .net core 使用ConcurrentTest组件对方法进行压力测试
- .net core中Quartz的使用方法
- .Net基础——程序集与CIL HttpClient封装方法 .Net Core 编码规范 C#中invoke和beginInvoke的使用 WebServeice 动态代理类
- .NET Core的CLI使用方法大全
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
- 在.NET Core类库中使用EF Core迁移数据库到SQL Server的方法
- .NET CORE中使用AutoMapper进行对象映射的方法
- 在.NET Core中使用异步编程的方法步骤
- .Net Core 使用EF Core方法
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
- c# ArrayList 的 Sort()方法的使用
- DateTime的一些使用方法
- 使用COM或者OLE的方法调用来控制Excel或者其他Office组件
- DEV-CPP中生成和使用DLL的方法
- RetroGuard的使用方法(转)
- 如何在DataTable中灵活使用Select方法