Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API ActionFilter modify returned value

Tags:

I have a Web API application that I need to get ahold of the return value of some of the API endpoints via an ActionFilter's OnActionExecuted method

I'm using a custom attribute to identify the endpoints that have data that I need to modify, but I can't seem to find the actual result object from within the HttpActionExecutedContext.

Thanks for any help!

like image 881
joe_coolish Avatar asked Oct 06 '12 14:10

joe_coolish


People also ask

Does .NET Web API supports action filter?

Web API includes filters to add extra logic before or after action method executes. Filters can be used to provide cross-cutting features such as logging, exception handling, performance measurement, authentication and authorization.


1 Answers

You can get the returned value through the Response.Content property. If your action has returned an object you can cast it to ObjectContent from where you can get the actual instance of the returned value:

public class MyFilterAttribute : ActionFilterAttribute {     public override void OnActionExecuted(HttpActionExecutedContext context)     {         var objectContent = context.Response.Content as ObjectContent;         if (objectContent != null)         {             var type = objectContent.ObjectType; //type of the returned object             var value = objectContent.Value; //holding the returned value         }     } } 
like image 163
nemesv Avatar answered Sep 27 '22 19:09

nemesv