我想将 appId 变量值发送到过滤器
// GET api/filter
[CustomFilter]
public IEnumerable<string> Get()
{
var appId = 123;
return new string[] { "value1", "value2" };
}
我可以使用 OnActionExecuting 或 OnActionExecuted 方法
public override void OnActionExecuting(HttpActionContext actionContext)
{
base.OnActionExecuting(actionContext);
//here i want to access appId value
}
我知道如何使用查询字符串访问参数值
最佳答案
在 Controller 操作方法中,设置请求对象的 Properties
字典中的值,如下所示:Request.Properties["AppId"] = 123;
。
在过滤器的 OnActionExecuted
方法中,按如下方式检索它:actionContext.Request.Properties["AppId"]
。
顺便说一句,如果在操作方法中设置了值,则必须使用过滤器的 OnActionExecuted
方法。 OnActionExecuting
方法在执行操作方法之前运行。
关于asp.net - 将变量从 Web API Controller 方法传递到过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25081946/