IExceptionFilter、IAsyncExceptionFilter

IExceptionFilter在它的作用范围内,当程序发生异常,可以接收到异常,对异常进行统一的处理。

当程序同时继承了IExceptionFilter,IAsyncExceptionFilter,则优先处理异步方法OnExceptionAsync

1、新建过滤器类CustomExceptionFilterAttribute继承Attribute, IExceptionFilter

 1 using Microsoft.AspNetCore.Mvc;
 2 using Microsoft.AspNetCore.Mvc.Filters;
 3 using Microsoft.AspNetCore.Mvc.ModelBinding;
 4 using Microsoft.AspNetCore.Mvc.ViewFeatures;
 5 
 6 namespace Project6.Utility.Filters
 7 {
 8     public class CustomExceptionFilterAttribute : Attribute, IExceptionFilter
 9     {
10         private readonly IModelMetadataProvider _IModelMetadataProvider;
11         public CustomExceptionFilterAttribute(IModelMetadataProvider modelMetadataProvider)
12         {
13             _IModelMetadataProvider= modelMetadataProvider;
14         }
15 
16         public void OnException(ExceptionContext context)
17         {
18            
19             if(IsAjaxRequest(context.HttpContext.Request))  //判断是否是Ajax异步请求,返回一个JSON
20             {
21                 context.Result = new JsonResult(new
22                 {
23                     Sucess = false,
24                     Message = context.Exception.Message
25                 });
26             }
27             else  //返回页面
28             {
29                 ViewResult result = new ViewResult { ViewName="~/Views/Shared/Error.cshtml"};
30                 result.ViewData = new ViewDataDictionary(_IModelMetadataProvider, context.ModelState);
31                 result.ViewData.Add("Exception", context.Exception);
32                 context.Result = result;  //赋值给Result程序就不在往后执行了
33             }
34         }
35 
36         //public Task OnExceptionAsync(ExceptionContext context)
37         //{
38         //    throw new NotImplementedException();
39         //}
40 
41         private bool IsAjaxRequest(HttpRequest request)
42         {
43             string header = request.Headers["X-Requested-With"];
44             return "XMLHttpRequest".Equals(header);
45         }
46     }
47 }
View Code

2、新建控制器CustomExceptionFilterController

 1 using Microsoft.AspNetCore.Mvc;
 2 using Project6.Utility.Filters;
 3 
 4 namespace Project6.Controllers
 5 {
 6     public class CustomExceptionFilterController : Controller
 7     {
 8         [TypeFilter(typeof(CustomExceptionFilterAttribute))]
 9         public IActionResult Index()
10         {
11             throw new Exception("抛出个异常测试");
12         }
13     }
14 }
View Code

3、修改/Views/Shared/Error.cshtml

@model ErrorViewModel
@{
    ViewData["Title"] = "Error";
    Exception exception = ViewData["Exception"] as Exception;
}

<h1 class="text-danger">Error.</h1>
@if(exception != null){
    <h1>@exception.Message</h1>
}
View Code

4、执行程序/CustomExceptionFilter

可以发现当程序抛出异常,会进入CustomExceptionFilterAttribute-->OnException,在OnException里面做统一处理。返回异常结果。

5、IExceptionFilter的作用范围

  a、action出现没有处理的异常

  b、Service层出现的异常,因为异常是从下往上抛出,Service属于下层

  c、构造器函数出现的异常

 

6、IExceptionFilter捕捉不到的异常

  a、Action里面已经有try\catch捕捉异常,则不会在进入IExceptionFilter

  b、View视图层抛出的异常

  c、不存在的URL,404捕捉不到

  d、其它Filter中出现的异常,有的能捕捉到,有的不能,比如IResourceFilter捕捉不到。

posted @ 2022-05-08 18:52  ziff123  阅读(307)  评论(0编辑  收藏  举报