Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 06, 2012 04:23 AM by priya_marwaha
Feb 29, 2012 06:38 PM|LINK
WebAPI preview 6 came with the full source and a bunch of samples - do we have something similar for Asp.Net WebAPI as well? More specifically, I'm looking for the equivalent of the TaskDispatcher sample - where the return type Task<T> of the method was
being converted to type T in the response.
Feb 29, 2012 06:50 PM|LINK
This source code is not yet available. Read the Faq for more details: http://wcf.codeplex.com/wikipage?title=WCF%20Web%20API%20is%20now%20ASP.NET%20Web%20API.
What exactly are you trying to accomplish with Task<T> in the new bits?
Feb 29, 2012 06:55 PM|LINK
Thanks again! I was trying to figure out how to change the return type of a method. So, if my method (action) has a return type MyCustomResponse<T>, I want to actually return just T to the caller. In the behavior (previous version), this as achieved by a)
modifying the operation description and b) changing the actual return type via an operation invoker.
Feb 29, 2012 08:44 PM|LINK
where the return type Task<T> of the method was being converted to type T in the response.
ASP.NET Web API already handles the case where you return Task<T>. Basically any response type gets wrapped into a Task<HttpResponseMessage>, regardless of whether your action method returns T, Task<T>, HttpContent, Task<HttpContent>, HttpResponseMessage,
Feb 29, 2012 09:01 PM|LINK
If it is a custom response (e.g, Dto<T>) then there are a couple of ways to accomplish this. I would first look at using an ActionFilter to change the return type.
Mar 01, 2012 04:03 AM|LINK
Thanks...but in my case, I want to wrap it in a custom response class, not Task.
Mar 01, 2012 04:24 AM|LINK
Thanks Dave...will try that and/or resultfilter - although I was thinking along the lines of altering response in a message handler. In the previous version, WebAPI used to throw an error if I only altered the response without changing the operation description.
Will need to see what it does now. I'm not an MVC person but I was thinking that if WebAPI complains, I may need to alter the action description via a custom descriptor etc. (no idea if that's even possible!).
Mar 05, 2012 11:52 AM|LINK
I've verified that I can change the response type at will - both from a delegating handler as well as the WebAPI specific ActionFilter. Any recommendations on which is the preferred way (since they can both do the job)?
Mar 05, 2012 02:45 PM|LINK
The Action filters seems to fit bettter in this scenario; you are modifying the action's result. Additionally, it is more discoverable for anyone reading your action code.
I view handlers as place for implementing concerns that are orthogonal to your action.
Mar 06, 2012 04:23 AM|LINK
Makes sense, thank you!