locked
PostAsync does not execute web API controller action RRS feed

  • Question

  • User-1787558873 posted

    Hi, I would like to call  my webAPI method but il does never fire

    Here is my controller action

            [Route("api/groups/Join")]
            [HttpPost]
            public async Task<HttpResponseMessage> Join(JoinGroupItem JoinGroupItem)
            {
                //TODO  create data here
    
                return await Task.Run(() => Request.CreateResponse(HttpStatusCode.Created, JoinGroupItem));
            }

    And I invoke action like this

     var item = new JoinGroupItem
                    {
                        Id = new Guid("92961370-bd5d-4ddf-9f9f-40940f87c31f"),
                        Email = "yvesgali"
                    };

     using (var client = new HttpClient())
                {
                   
                    client.BaseAddress = new Uri("http://localhost:9456/");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    var response = await client.PostAsJsonAsync("api/groups/Join", item);
                    if (response.IsSuccessStatusCode)
                    {
                        // Get the URI of the created resource.
                        Uri returnUrl = response.Headers.Location;
                    }
                }

    PostAsJsonAsync    and PostAsync  does not execute controller action

    my web api baseURL is : http://localhost:9456/

    Whats wrong ?

    Wednesday, May 11, 2016 6:23 PM

Answers

  • User36583972 posted

    Hi Olivier DUBOIS,

    You can try the following methods.

    1: You can add Microsoft.AspNet.WebApi.Client nuget package in your project.

    2: Try the following code

    client:

                 using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
                {
                    var url = string.Format("{0}/{1}?", "http://localhost:29679/", "api/values");
                    JoinGroupItem paramList = new JoinGroupItem() {  id="1", name="hello"};
                    HttpResponseMessage response = client.PostAsJsonAsync(url, paramList).Result;
                    var responseData = response.Content.ReadAsStringAsync().Result;
                    var status = ((int)response.StatusCode).ToString();
                    if (status == "200")
                    {
                        var result = default(Object);
                        try
                        {
                            if (!string.IsNullOrEmpty(responseData))
                                result = JsonConvert.DeserializeObject<Object>(responseData);
                            Response.Write("Result: " + result);
                         
                        }
                        catch
                        {
                        }
                    }
                    else
                    {
                    }
                }
    

    API:

     [AllowAnonymous]
            public HttpResponseMessage Post([FromBody]JoinGroupItem msg)
            {
                HttpResponseMessage response = new HttpResponseMessage { StatusCode = HttpStatusCode.OK };
                response.Content = new ObjectContent<JoinGroupItem>(msg, new JsonMediaTypeFormatter(), "application/json");
    
                return response;
            }

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 12, 2016 2:29 AM