Can I modify response content before the formatter processes it?

956 views Asked by At

I have an ASP.NET Web Api 2.2 project where I would like to take the response from all controllers and wrap it in a wrapper object with some metadata. Then I would like my custom Media Type formatter (particularly my custom JsonMediaTypeFormatter) to use that extra metadata to do some custom serialization.

I tried creating a DelegationHandler to do the wrapping, but that happens after the formatter in the pipeline. Is there another way to intercept the response from all controller actions, wrap the response in another object and then have the custom JsonMediaTypeFormatter process it?

1

There are 1 answers

1
Omar.Alani On BEST ANSWER

Have You tried using an ActionFilterAttribute where you can override the OnActionExecuting/OnActionExecuted methods to process/generate the action response using the actionContext.

Have a look into this good article.

Also look at this question.

Hope that helps.