1

As per this question here, ASP.NET MVC Pass object from Custom Action Filter to Action

, (which is for MVC), is there a similar object that we can add items or values to from within

public override void OnActionExecuting(HttpActionContext actionContext)
{
}

, (which is for WebAPI), and access it seconds later in the Controller's action method itself ?

joedotnot
  • 4,810
  • 8
  • 59
  • 91

1 Answers1

0

This works:

set data:

public class MyAwesomeFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        //add stuff here.. to be accessed later
        actionContext.Request.Properties.Add("myKey69", myValue);

    }       
}   

retrieve data:

[MyAwesomeFilter]
public IHttpActionResult MyController( [FromBody] string myParmStr ) {

    //retrieve the obj you inserted in filter...
    obj myValue = null;
    if (Request.Properties.TryGetValue("myKey69", out myValue)) {
        //logic here...
    }

}
joedotnot
  • 4,810
  • 8
  • 59
  • 91
  • Now would anyone like to try this for dotnet core, and post a similar answer? Or is it exactly the same? – joedotnot Nov 26 '19 at 07:46
  • I am using v4.8, and I am getting 'LogActionFilterAttribute.OnActionExecuting(HttpActionContext)': no suitable method found to override – Doug Moore Jul 19 '22 at 14:29
  • Turns out I was using the wrong instance of ActionFilterAttribute. I was using the one from Mvc instead of System.Web.Http.Filters – Doug Moore Jul 19 '22 at 14:32