asp.net mvc - Modify HttpContent (actionExecutedContext.Response.Content) in OnActionExecuted method of WebApi's ActionFilterAttribute -


my webapi action method returns iqueryable , want modify (apply paging & filtering) through actionfilterattribute in asp.net webapi(not mvc). following thread got how access passed model :

.net web api iactionfilter.onactionexecuted return type

but how can change/replace whole model else?

i found it!

first should cast actionexecutedcontext.actioncontext.response.content objectcontent (you should have refrence system.net.http.formatting.dll file in project)

after can following :

public override void onactionexecuted(system.web.http.filters.httpactionexecutedcontext actionexecutedcontext) {     ienumerable model = null;     actionexecutedcontext.response.trygetcontentvalue(out model);     if (model != null)     {         iqueryable modelquery = model.asqueryable();          //do modelquery modification/replacement          (actionexecutedcontext.actioncontext.response.content objectcontent).value = modelquery;     }      base.onactionexecuted(actionexecutedcontext); } 

note : use trygetcontentvalue method need import using system.net.http; namespace although calling methoud not important in above code.

:: update ::

if need change content's value type (for example returning string instead of iqueryable) can't change value. should create new content :

var result = "something new!"; var oldobjectcontent = (actionexecutedcontext.actioncontext.response.content objectcontent); var newcontent = new objectcontent<string>(result, oldobjectcontent.formatter); actionexecutedcontext.actioncontext.response.content = newcontent; 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -