WebApi 通过ActionFilter 来响应请求

如果在ActionFilterAttribute的OnActionExecuting事件中返回一个 HttpResponseMessage 作为 参数HttpActionContext 的Response属性

那么会直接返回,甚至不会执行action。比如 验证未通过,直接返回 Response

 

如果在OnActionExecuted 进行返回Response,那么会执行全部的 OnActionExecuted 事件。

同时如果action发生异常,那么还会覆盖掉action的 exception

 

 

 

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()
        {
            return "action正常执行";
        }
    }

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


// 同一个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 08:08  zq爱生活爱代码  阅读(217)  评论(0编辑  收藏  举报