【问题标题】:Passing variable from web api controller method to filter从 web api 控制器方法传递变量到过滤器
【发布时间】:2014-08-01 13:58:30
【问题描述】:

我想将 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

}

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

【问题讨论】:

  • 好的,实际上问题是我必须将 appid 的动态值发送到过滤器,我将如何实现这一点
  • 价值从何而来?
  • 我也有同样的问题。请帮帮我

标签: asp.net asp.net-web-api action-filter


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 2012-07-19
    • 2013-08-22
    相关资源
    最近更新 更多