将变量从 Web API 控制器方法传递到过滤器

2 asp.net action-filter asp.net-web-api

我想将 appId 变量值发送到过滤器

// GET api/filter
[CustomFilter]
public IEnumerable<string> Get()
{

   var  appId = 123;
    return new string[] { "value1", "value2" };
}
Run Code Online (Sandbox Code Playgroud)

我可以使用 OnActionExecuting 或 OnActionExecuted 方法

public override void OnActionExecuting(HttpActionContext actionContext)
{
            base.OnActionExecuting(actionContext);

           //here i want to access appId value

}
Run Code Online (Sandbox Code Playgroud)

我知道如何使用查询字符串访问参数值

Bad*_*dri 5

从控制器操作方法中,在请求对象的字典中设置值Properties,如下所示:Request.Properties["AppId"] = 123;

OnActionExecuted过滤器的方法中,像这样检索它:actionContext.Request.Properties["AppId"]

OnActionExecuted顺便说一句,如果在操作方法中设置了值,则必须使用过滤器的方法。该OnActionExecuting方法在执行操作方法之前运行。