WebApi ExceptionFilterAttribute 响应流程

1.如果是action中发生异常,那么 会先先逆着顺序执行 actionfilter的OnActionExecuted事件,最后再逆着执行 ExceptionFilterAttribute 的 OnException事件

通过参数HttpActionExecutedContext.Exception 获取异常信息

2.如果是ActionFilterAttribute 中OnActionExecuting发生异常,会执行已执行过的ActionFilterAttribute 的OnActionExecuted,然后再逆着执行 ExceptionFilterAttribute 的 OnException事件

3.如果是ActionFilterAttribute 中OnActionExecuted发发生异常,会执行完所有的OnActionExecuted事件后,再逆着执行 ExceptionFilterAttribute 的 OnException事件

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Web.Http.ModelBinding;
using System.Web.Http.ValueProviders;

namespace WebApi.Controllers
{
    public class DemoController : ApiController
    {
        [Foobar]
        [Foobar2]
        [Foobar3]
        [ExceptionF]
        [ExceptionT]
        public string Get()
        {
            //throw new Exception("自定义异常");
            return "GET调用成功";
        }
    }

    public class FoobarAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            actionContext.ModelState.Add("Foo", new ModelState() { Value = new ValueProviderResult("Foo", "Foo", null)});
        }
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            //actionExecutedContext.Response = null;
            //actionExecutedContext.Exception = new Exception("在执行ActionFilter的OnActionExecuted方法过程中发生异常");
            //throw new Exception("自定义异常Foobar2Attribute");
        }

    }
    public class Foobar2Attribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            actionContext.ModelState.Add("Foo2", new ModelState() { Value = new ValueProviderResult("Foo", "Foo", null) });
            
        }
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            //actionExecutedContext.Response = null;
            //actionExecutedContext.Exception = new Exception("在执行ActionFilter的OnActionExecuted方法过程中发生异常");
            //actionExecutedContext.ActionContext.ModelState.Add("Foo2end", new ModelState() { Value = new ValueProviderResult("Foo2end", "Foo2end", null) });
            throw new Exception("自定义异常Foobar2Attribute");
        }
    }
    public class Foobar3Attribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            actionContext.ModelState.Add("Foo3", new ModelState() { Value = new ValueProviderResult("Foo", "Foo", null) });
        }
        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
        {
            //actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse<string>(HttpStatusCode.OK, "FooOnActionExecuting");
        }
    }

    public class ExceptionFAttribute : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            Console.WriteLine(actionExecutedContext.Exception.Message);
        }
    }
    public class ExceptionTAttribute : ExceptionFilterAttribute
    {
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            Console.WriteLine(actionExecutedContext.Exception.Message);
        }
    }
}


// 同一个action上多个actionfilter 会按照 顺序调用 OnActionExecuting 事件
//action 执行完毕后,会生成httpresponsemessage作为 OnActionExecuted 事件参数 HttpActionExecutedContext 的response属性
// 之后 会逆向调用 OnActionExecuted 事件

// 如果action里面发生异常,那么将会传递给 OnActionExecuted 事件参数 HttpActionExecutedContext,该参数 response属性会设定null,同时 Exception会设定成action的异常


// 当action发生异常时,
//如果是OnActionExecuting 发生异常,那么后续会执行已经成功执行过 actionfilter的OnActionExecuted事件
//如果是OnActionExecuted 发送异常,那么会覆盖掉action里面发生的异常,同时后续 actionfilter的OnActionExecuted事件都会正常执行

  

posted @ 2021-11-17 16:39  zq爱生活爱代码  阅读(152)  评论(0编辑  收藏  举报