No worries, there isn't a onresultexecuted method for the asp.net web api which i would assume has the response data in context so not sure the best way to go about this?
No worries, there isn't a onresultexecuted method for the asp.net web api which i would assume has the response data in context so not sure the best way to go about this?
I just setup a quick sample and I am getting a non-null Content in my filter's OnActionExecuted.
The current response i am trying to catch and log is a string response from a web service. Is there actually a way to do this from the filter context? Sorry for my confusing first question - hopefully its a bit more understandable now what i am actually
trying to do.
You want the post-processing side of the message handler, so this means in your delegating message handler calling the base.SendAsync and then get the results with ContinueWith -- I think there's an example on that page I linked that shows this... yep, here's
the snippet from that page:
public class CustomHeaderHandler : DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
return base.SendAsync(request, cancellationToken).ContinueWith(
(task) =>
{
HttpResponseMessage response = task.Result;
response.Headers.Add("X-Custom-Header", "This is my custom header.");
return response;
}
);
}
}
gdperkins
Member
3 Points
12 Posts
Filter context - getting the result
Apr 27, 2012 01:09 PM|LINK
Hi,
I have a custom filter like this:
public override OnActionExecuted(HttpActionExecutedContext context) { context.Result.Content //is always null base.OnActionExecuted(context); }At what stage is the Result.Content populated? I want to be able to check the action result for logging purposes etc.
Is there anyway i can do this?
Many Thanks in advance.
BrockAllen
All-Star
27534 Points
4907 Posts
MVP
Re: Filter context - getting the result
Apr 27, 2012 01:12 PM|LINK
Oh sorry -- I should have noticed this was WebApi forum. I gave the MVC answer, not the WebApi answer.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
gdperkins
Member
3 Points
12 Posts
Re: Filter context - getting the result
Apr 27, 2012 01:20 PM|LINK
No worries, there isn't a onresultexecuted method for the asp.net web api which i would assume has the response data in context so not sure the best way to go about this?
BrockAllen
All-Star
27534 Points
4907 Posts
MVP
Re: Filter context - getting the result
Apr 27, 2012 01:33 PM|LINK
I just setup a quick sample and I am getting a non-null Content in my filter's OnActionExecuted.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
gdperkins
Member
3 Points
12 Posts
Re: Filter context - getting the result
Apr 27, 2012 02:33 PM|LINK
The current response i am trying to catch and log is a string response from a web service. Is there actually a way to do this from the filter context? Sorry for my confusing first question - hopefully its a bit more understandable now what i am actually trying to do.
BrockAllen
All-Star
27534 Points
4907 Posts
MVP
Re: Filter context - getting the result
Apr 27, 2012 03:19 PM|LINK
Maybe the best place for logging is in a message handler.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
gdperkins
Member
3 Points
12 Posts
Re: Filter context - getting the result
Apr 28, 2012 03:26 PM|LINK
Hi Brock,
I am looking at the request object when using a message handler, how can i log the content before its sent to the action result?
Message handlers do seemer a better fit than using a filter.
Thanks.
BrockAllen
All-Star
27534 Points
4907 Posts
MVP
Re: Filter context - getting the result
Apr 28, 2012 03:30 PM|LINK
You want the post-processing side of the message handler, so this means in your delegating message handler calling the base.SendAsync and then get the results with ContinueWith -- I think there's an example on that page I linked that shows this... yep, here's the snippet from that page:
public class CustomHeaderHandler : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { return base.SendAsync(request, cancellationToken).ContinueWith( (task) => { HttpResponseMessage response = task.Result; response.Headers.Add("X-Custom-Header", "This is my custom header."); return response; } ); } }DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
gdperkins
Member
3 Points
12 Posts
Re: Filter context - getting the result
Apr 28, 2012 03:40 PM|LINK
Yeh that makes sense.
Thankyou again you've been great help