为什么异常处理选择中间件?

传统的asp.net可以采用异常过滤器的方式处理异常,在asp.net core中,是以多个中间件连接而成的管道形式处理请求的,不过常用的五大过滤器得以保留,同样可以采用异常过滤器处理异常,但是异常过滤器不能处理mvc中间件以外的异常,为了全局统一考虑,采用中间件处理异常更为合适

为什么选择自定义异常中间件?

先来看看asp.net core 内置的三个异常处理中间件 developerexceptionpagemiddleware, exceptionhandlermiddleware,statuscodepagesmiddleware

1.developerexceptionpagemiddleware

能给出详细的请求/返回/错误信息,因为包含敏感信息,所以仅适合开发环境

2.exceptionhandlermiddleware (蒋神博客:)

仅处理500错误

3.statuscodepagesmiddleware (蒋神博客:)

能处理400-599之间的错误,但需要response中不能包含内容(contentlength=0 && contenttype=null,经实验不能响应mvc里未捕获异常)

由于exceptionhandlermiddleware和statuscodepagesmiddleware的各自的限制条件,两者需要搭配使用。相比之下自定义中间件更加灵活,既能对各种错误状态进行统一处理,也能按照配置决定处理方式。

customexceptionmiddleware

首先声明异常中间件的配置类

/// <summary>
 /// 异常中间件配置对象
 /// </summary>
 public class customexceptionmiddlewareoption
 {
 public customexceptionmiddlewareoption(
  customexceptionhandletype handletype = customexceptionhandletype.jsonhandle,
  ilist<pathstring> jsonhandleurlkeys = null,
  string errorhandingpath = "")
 {
  handletype = handletype;
  jsonhandleurlkeys = jsonhandleurlkeys;
  errorhandingpath = errorhandingpath;
 }

 /// <summary>
 /// 异常处理方式
 /// </summary>
 public customexceptionhandletype handletype { get; set; }

 /// <summary>
 /// json处理方式的url关键字
 /// <para>仅handletype=both时生效</para>
 /// </summary>
 public ilist<pathstring> jsonhandleurlkeys { get; set; }

 /// <summary>
 /// 错误跳转页面
 /// </summary>
 public pathstring errorhandingpath { get; set; }
 }

 /// <summary>
 /// 错误处理方式
 /// </summary>
 public enum customexceptionhandletype
 {
 jsonhandle = 0, //json形式处理
 pagehandle = 1, //跳转网页处理
 both = 2  //根据url关键字自动处理
 }

声明异常中间件的成员

/// <summary>
 /// 管道请求委托
 /// </summary>
 private requestdelegate _next;

 /// <summary>
 /// 配置对象
 /// </summary>
 private customexceptionmiddlewareoption _option;

 /// <summary>
 /// 需要处理的状态码字典
 /// </summary>
 private idictionary<int, string> exceptionstatuscodedic;

 public customexceptionmiddleware(requestdelegate next, customexceptionmiddlewareoption option)
 {
  _next = next;
  _option = option;
  exceptionstatuscodedic = new dictionary<int, string>
  {
  { 401, "未授权的请求" },
  { 404, "找不到该页面" },
  { 403, "访问被拒绝" },
  { 500, "服务器发生意外的错误" }
  //其余状态自行扩展
  };
 }

异常中间件主要逻辑

public async task invoke(httpcontext context)
 {
  exception exception = null;
  try
  {
  await _next(context); //调用管道执行下一个中间件
  }
  catch (exception ex)
  {
  context.response.clear(); 
  context.response.statuscode = 500; //发生未捕获的异常,手动设置状态码
  exception = ex;
  }
  finally
  {
  if (exceptionstatuscodedic.containskey(context.response.statuscode) && 
   !context.items.containskey("exceptionhandled")) //预处理标记
  {
   var errormsg = string.empty;
   if (context.response.statuscode == 500 && exception != null)
   {
   errormsg = $"{exceptionstatuscodedic[context.response.statuscode]}\r\n{(exception.innerexception != null ? exception.innerexception.message : exception.message)}";
   }
   else
   {
   errormsg = exceptionstatuscodedic[context.response.statuscode];
   }
   exception = new exception(errormsg);
  }

  if (exception != null)
  {
   var handletype = _option.handletype;
   if (handletype == customexceptionhandletype.both) //根据url关键字决定异常处理方式
   {
   var requestpath = context.request.path;
   handletype = _option.jsonhandleurlkeys != null && _option.jsonhandleurlkeys.count(
    k => context.request.path.startswithsegments(k, stringcomparison.currentcultureignorecase)) > 0 ?
    customexceptionhandletype.jsonhandle :
    customexceptionhandletype.pagehandle;
   }
   
   if (handletype == customexceptionhandletype.jsonhandle)
   await jsonhandle(context, exception);
   else
   await pagehandle(context, exception, _option.errorhandingpath);
  }
  }
 }

 /// <summary>
 /// 统一格式响应类
 /// </summary>
 /// <param name="ex"></param>
 /// <returns></returns>
 private apiresponse getapiresponse(exception ex)
 {
  return new apiresponse() { issuccess = false, message = ex.message };
 }

 /// <summary>
 /// 处理方式:返回json格式
 /// </summary>
 /// <param name="context"></param>
 /// <param name="ex"></param>
 /// <returns></returns>
 private async task jsonhandle(httpcontext context, exception ex)
 {
  var apiresponse = getapiresponse(ex);
  var serialzestr = jsonconvert.serializeobject(apiresponse);
  context.response.contenttype = "application/json";
  await context.response.writeasync(serialzestr, encoding.utf8);
 }

 /// <summary>
 /// 处理方式:跳转网页
 /// </summary>
 /// <param name="context"></param>
 /// <param name="ex"></param>
 /// <param name="path"></param>
 /// <returns></returns>
 private async task pagehandle(httpcontext context, exception ex, pathstring path)
 {
  context.items.add("exception", ex);
  var originpath = context.request.path;
  context.request.path = path; //设置请求页面为错误跳转页面
  try
  {
  await _next(context); 
  }
  catch { }
  finally
  {
  context.request.path = originpath; //恢复原始请求页面
  }
 }

使用扩展类进行中间件注册

public static class customexceptionmiddlewareextensions
 {

 public static iapplicationbuilder usecustomexception(this iapplicationbuilder app, customexceptionmiddlewareoption option)
 {
  return app.usemiddleware<customexceptionmiddleware>(option);
 }
 }

在startup.cs的configuref方法中注册异常中间件

 app.usecustomexception(new customexceptionmiddlewareoption(
   handletype: customexceptionhandletype.both, //根据url关键字决定处理方式
   jsonhandleurlkeys: new pathstring[] { "/api" },
   errorhandingpath: "/home/error"));

接下来我们来进行测试,首先模拟一个将会进行页面跳转的未经捕获的异常

访问/home/about的结果

访问/home/test的结果 (该地址不存在)

ok异常跳转页面的方式测试完成,接下来我们测试返回统一格式(json)的异常处理,同样先模拟一个未经捕获的异常

访问/api/token/gettesterror的结果

访问/api/token/test的结果 (该地址不存在)

访问/api/token/getvalue的结果 (该接口需要身份验证)

测试完成,页面跳转和统一格式返回都没有问题,自定义异常中间件已按预期工作

需要注意的是,自定义中间件会响应每个http请求,所以处理逻辑一定要精简,防止发生不必要的性能问题

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对www.887551.com的支持。