This project has moved and is read-only. For the latest updates, please go here.


Ts-Ng: Return types for WebApi Methods with [ResponseType()] Attribute


For methods generated on client side only:

By Default, the generated typescript for angular returns Observable<Any> if you have return type set up to an interface e.g. in your webapi controller if you have :
public async Task<IHttpActionResult> GetAllBunnies()
In this case resulting service method on angular looks like
 getAllBunnies(): Observable<any>{......}
However, as a part of practice if you define ResponseType Attribute, it should be like :
 getAllBunnies(): Observable<Array<Bunny>>{......}
For anyone facing this issue : Just change following in source

WebApiClientGen > ClientApiTsFunctionGenBase.cs > Line 44 :

ReturnType =  description.ActionDescriptor.ReturnType;
ReturnType = description.ResponseDescription?.ResponseType  ?? description.ActionDescriptor.ReturnType;
Will Fix the issue.

Closed Mar 26, 2017 at 5:58 AM by zijianhuang


zijianhuang wrote Sep 8, 2016 at 3:28 AM

Not sure what could be the benefit of using HttpResponseMessage or IHttpActionResult accompanied with ResponseTypeattribute, rather than using a strongly return types like string, int and decimal etc.

The only extra thing that HttpResponseMessage could carryout is http status code and reason phase etc.
However, these could be implemented through something like

throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.BadRequest) { ReasonPhrase = "parametersNull" });

I suppose such programming style is more consistent / abstract with strongly typed Web API and client API, so you Web API codes have less explicit HTTP handlings which have been largely encapsulated by Web API 2.x runtime.

zijianhuang wrote Sep 9, 2016 at 12:57 PM

fixes along for client API in C#. This is probably good for legacy Web API 1 codes still using ResponseTypeAttribute and the developers don't want to modify codes into more abstract v2 style.