asp.net mvc - Transform Results Of Web API Actions -


how transform results of controller actions before framework formats it?

suppose have controller like:

public class controller : system.web.http.apicontroller {     public object getsomething() {         return new { value = "something" };     } } 

i want able build this:

public class actionresultfilter {     public object invokeaction(action action) {         object actionresult = action.invoke();         if (actionresult == null) {             return new { value = "nothing" };         }         return actionresult;     } } 

the key things want after action executed, before result converted httpresponsemessage don't have handle serialization.

here's i've tried:

  • actionfilterattribute late. result httpresponsemessage.
  • i don't know how invoke actions ihttpactioninvoker. plus, don't want return httpresponsemessage.
  • controlleractioninvoker doesn't seem give me response anywhere.

from http://www.asp.net/posters/web-api/asp.net-web-api-poster-grayscale.pdf, looks want between controller action , onactionexecuted.

you can httpcontent in httpresponsemessage , check if objectcontent within actionfilterattribute:

public class yourfilternameattribute : actionfilterattribute {     public override void onactionexecuted(httpactionexecutedcontext context)     {         var objectcontent = context.response.content objectcontent         if(objectcontent != null && objectcontent.value == null)          {             context.response = context.request                                       .createresponse(httpstatuscode.notfound,                                                        new { value = "nothing" });         }     } } 

this minimal example (with minimal error checking , limited scenario) should started on right path when comes intercepting , rewriting responses action filters.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -