How to send a List of objects to asp.net core web api

Seth Isaacks 11 Reputation points
2022-06-13T14:06:38.973+00:00

Hello

I have a asp.net core web api, that I calling from Xamarin. The normal CRUD methods work fine. I am trying however to add a method to the web api to send a list of objects. However I keep getting an error stating (see below). Ive also included my client side code and the method on the web api. I have googled and googled and googled, I cannot figure out what I am doing wrong.

My client side code looks like:
public async Task<bool> DiffSyncAsync(string jsonUpate)
{
var httpClient = new HttpClient();
HttpContent content = new StringContent(jsonUpate);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var result = await httpClient.PostAsync(WebServiceURL + "DiffertialSync", content);
return result.IsSuccessStatusCode;
}

My api method looks like:
[Route("api/vehicles/DiffertialSync")]
public async Task<IActionResult> PostVehicles([FromBody] List<Vehicle> vehicles)
{
if (vehicles == null || _context.Vehicle == null)
{
return NotFound();
}

        try  
        {  
            //List<Vehicle> Vehicles = JsonConvert.DeserializeObject<List<Vehicle>>(vehiclesJson);  

            //if (Vehicles != null)  
            //{  
            //    foreach (var vehicle in Vehicles)  
            //    {  
            //        _context.Entry(vehicle).State = EntityState.Modified;  
            //        await _context.SaveChangesAsync();  
            //    }  
            //    return Ok(true);  
            //}  
               
            return Ok(false);  
             
        }  
        catch (Exception)  
        {  

            throw;  
        }  
    }  

statusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Date: Mon, 13 Jun 2022 14:00:48 GMT
Server: Microsoft-IIS/10.0
Set-Cookie: ARRAffinity=8a89f47e19228123334b19b17c93837ee63c5f8af4981359f4abaa40ce6cbea9;Path=/;HttpOnly;Secure;Domain=myautoapi20220609112603.azurewebsites.net
Set-Cookie: ARRAffinitySameSite=8a89f47e19228123334b19b17c93837ee63c5f8af4981359f4abaa40ce6cbea9;Path=/;HttpOnly;SameSite=None;Secure;Domain=myautoapi20220609112603.azurewebsites.net
X-Android-Received-Millis: 1655128849680
X-Android-Response-Source: NETWORK 405
X-Android-Selected-Protocol: http/1.1

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2022-06-13T14:30:28.047+00:00

    The error is telling you that you're trying to call an endpoint and the method (POST) isn't supported on that endpoint. In your API controller action add HttpPost as an attribute.

       [HttpPost("api/vehicles/DiffertialSync")]  
       public async Task<IActionResult> PostVehicles([FromBody] List<Vehicle> vehicles)  
       {  
       }  
    

    Note also that you should be using the strongly typed ActionResult<T> return type and not IActionResult. This will help enforce compiler checking that you're returning the data you expect. It would be very bad for your API to return different types accidentally as the client won't be able to handle that.


  2. Seth Isaacks 11 Reputation points
    2022-06-13T16:42:00.553+00:00

    @Michael Taylor okay well it will not work if I remove the curly braces......so what am I doing wrong then.....


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.