WebApi ActionFilterAttribute 执行顺序以及发生异常时的执行顺序

 

// 同一个action上多个actionfilter 会按照 顺序调用 OnActionExecuting 事件,

注意 OnActionExecuting  发生在 Model参数绑定之后,可以在OnActionExecuting 里面验证 actionContext.ModelState.IsValid ,如果是FALSE,那么返回一个badresponse

 


//action 执行完毕后,会生成httpresponsemessage作为 OnActionExecuted 事件参数 HttpActionExecutedContext 的response属性
// 之后 会逆向调用 OnActionExecuted 事件

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


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

 

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]
        public string Get()
        {
            throw new Exception("自定义异常");
        }
    }

    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方法过程中发生异常");
            actionExecutedContext.ActionContext.ModelState.Add("Fooend", new ModelState() { Value = new ValueProviderResult("Fooend", "Fooend", null) });
        }

    }
    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) });
        }
    }
    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 = null;
            //actionExecutedContext.Exception = new Exception("在执行ActionFilter的OnActionExecuted方法过程中发生异常");
            actionExecutedContext.ActionContext.ModelState.Add("Foo3end", new ModelState() { Value = new ValueProviderResult("Foo3end", "Foo3end", null) });
        }
    }
}


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

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

  

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