Search code examples
c#.netasp.net-core.net-coreaction-filter

ActionFilter Get API Response Object


How do I get the value of an actual Object in ActionFilterAttribute? In the Debugger, I see value in actionExecutedContext.Result.Value.

However, when trying to actually type them in as

actionExecutedContext.Result.Value or actionExecutedContext.Result.GetValue(), it cannot recognize them.

I need the actual object, so it can be sent to one of my Static Data methods, which accepts an inputs, and multiplies all numbers by 2.

public async Task OnActionExecutionAsync(ActionExecutingContext actionContext, ActionExecutionDelegate next)
{
   var actionExecutedContext= await next();
   // ObjectDataConverter.MultiplyIntegersBy2(test.Result.Value);
}

enter image description here

actionExecutedContext.Result.Value error CS1061: 'IActionResult' does not contain a definition for 'Value' and no accessible extension method 'Value' accepting a first argument of type 'IActionResult' could be found (are you missing a using directive or an assembly reference?)

Resources: Read Asp.Net Core Response body in ActionFilterAttribute


Solution

  • You need to cast actionExecutedContext.Result to ObjectResult and use ObjectResult.Value if cast is successful:

    var objResult = actionExecutedContext.Result as ObjectResult;
    if (objResult != null && objResult.Value != null) {
      // process objResult.Value here
    }