Skip to content

ActionResult

fonlow edited this page Sep 1, 2018 · 2 revisions

The main focus of WebApiClientGen is to support strongly typed Web API. And if you have non-typed Web API functions, generally you should put them into some dedicated controllers and exclude those controllers from the CodeGen as defined in CodeGen.json. If the Web API functions are to have very dynamic behaviors, only hand-crafted client codes could possibly handle such behaviors.

Occasionally you may find legitimate cases to mix some non-typed API functions with other typed functions. WebApiClientGen provides limited or restricted supports for non-typed returns.

IHttpActionResult

Web API

        [HttpGet]
        [Route("ActionResult")]
        public IHttpActionResult GetActionResult()
        {
            return Ok("abcdefg");
        }

        [HttpPost]
        [Route("ActionResult")]
        public IHttpActionResult PostActionResult()
        {
            return Ok("abcdefg");
        }

        [HttpPost]
        [Route("PostActionResult2")]
        public IHttpActionResult PostActionResult2([FromBody] string s)
        {
            return Ok("abcdefg");
        }

        [HttpPost]
        [Route("PostActionResult3")]
        public IHttpActionResult PostActionResult3(DemoWebApi.DemoData.Person person)
        {
            return Ok(person.Name);
        }

        [HttpGet]
        [Route("ActionStringResult")]
        [System.Web.Http.Description.ResponseType(typeof(string))]
        public IHttpActionResult GetActionStringResult()
        {
            return Ok("abcdefg");
        }

C# client API:

        public async Task<System.Net.Http.HttpResponseMessage> GetActionResultAsync();
      
        public async Task<string> GetActionStringResultAsync();

        public async Task<System.Net.Http.HttpResponseMessage> PostActionResultAsync();

        public async Task<System.Net.Http.HttpResponseMessage> PostActionResult2Async(string s);

        public async Task<System.Net.Http.HttpResponseMessage> PostActionResult3Async(DemoWebApi.DemoData.Client.Person person);

        public async Task<string> GetActionStringResultAsync();

Angular TypeScript client API:

        getActionResult(): Observable<HttpResponse<string>> {
            return this.http.get(this.baseUri + 'api/SuperDemo/ActionResult', { observe: 'response', responseType: 'text' });
        }

        postActionResult(): Observable<HttpResponse<string>> {
            return this.http.post(this.baseUri + 'api/SuperDemo/ActionResult', null, { observe: 'response', responseType: 'text' });
        }

        postActionResult2(s: string): Observable<HttpResponse<string>> {
            return this.http.post(this.baseUri + 'api/SuperDemo/PostActionResult2', JSON.stringify(s), { headers: { 'Content-Type': 'application/json;charset=UTF-8' }, observe: 'response', responseType: 'text' });
        }

        postActionResult3(person: DemoWebApi_DemoData_Client.Person): Observable<HttpResponse<string>> {
            return this.http.post(this.baseUri + 'api/SuperDemo/PostActionResult3', JSON.stringify(person), { headers: { 'Content-Type': 'application/json;charset=UTF-8' }, observe: 'response', responseType: 'text' });
        }

        getActionStringResult(): Observable<string> {
            return this.http.get<string>(this.baseUri + 'api/SuperDemo/ActionStringResult');
        }

HttpResponseMessage

Web API:

        [HttpGet]
        [Route("TextStream")]
        public HttpResponseMessage GetTextStream()
        {
            var stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("abcdefg"));
            {
                var content = new StreamContent(stream);
                content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
                return new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = content
                };
            }
        }

C# Client API:

    public async Task<System.Net.Http.HttpResponseMessage> GetTextStreamAsync();

Angular TypeScript Client API:

        getTextStream(): Observable<HttpResponse<Blob>> {
            return this.http.get(this.baseUri + 'api/SuperDemo/TextStream', { observe: 'response', responseType: 'blob' });
        }

.NET Core Web API

In .NET Core Web API, there are Microsoft.AspNetCore.Mvc.IActionResult and Microsoft.AspNetCore.Mvc.ActionResult. For the first one, the client codes are identical to what generated for System.Web.Http.IHttpActionResult. For the second one, the client codes identical to what generated for System.Net.Http.HttpResponseMessage.

You can’t perform that action at this time.