user2426842
user2426842

Reputation:

Passing variable from web api controller method to filter

I want to send appId variable value to filter

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

   var  appId = 123;
    return new string[] { "value1", "value2" };
}

I can use either OnActionExecuting or OnActionExecuted method

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

           //here i want to access appId value

}

I know how to access parameter values using querystring

Upvotes: 2

Views: 4615

Answers (1)

From the controller action method, set the value in the Properties dictionary of the request object, like this: Request.Properties["AppId"] = 123;.

In the OnActionExecuted method of the filter, retrieve it like this: actionContext.Request.Properties["AppId"].

BTW, you must use the OnActionExecuted method of the filter, if the value is set in the action method. The OnActionExecuting method runs before the action method is executed.

Upvotes: 5

Related Questions