Search code examples
c#asp.netasp.net-web-apiodata

odata action accepting list parameter is always null


I have a problem with an odata controller.

I wish to send a list of Address-objects, but the received parameter is always null.

My end-point looks like this:

    [EnableQuery]
    public IHttpActionResult SetCoordinatesOnAddressList(IEnumerable<Address> addresses)
    {
        var result = addresses.Select(address => _coordinates.GetAddressCoordinates(address)).ToList();
        return Ok(result);
    } 

I have this set up in WebApiConfig

builder.EntityType<Address>().Collection
            .Action("SetCoordinatesOnAddressList")
            .ReturnsFromEntitySet<Address>("Addresses");

I am using Postman to send requests.

My request is sent as a post-request and looks like this:

[{"StreetName":"Some street","StreetNumber":"8","ZipCode":1234,"Town":"Some town"}]

I have a different end-point which takes a single Address instead of an IEnumerable, and that works with the same json-request (without []), so the fields in the json are correct.

So to sum up: My endpoint for an IEnumerable always receives null. Any ideas what might be causing it?

Thank you.

Edit:

I tried changing my end-point to accept an AddressDTO instead, like this:

public IHttpActionResult SetCoordinatesOnAddressList(AddressDTO addresses)
{
    return Ok(2);
}

and this is an AddressDTO:

public class AddressDTO
{
    public IEnumerable<Address> Addresses { get; set; }
}

If I try to post this:

{"Addresses": [{"StreetName":"Some street","StreetNumber":"8","ZipCode":1234,"Town":"Some town"}]}

I get this error:

{
  "error": {
    "code": "",
    "message": "The request entity's media type 'application/json' is not supported for this resource.",
    "innererror": {
      "message": "No MediaTypeFormatter is available to read an object of type 'AddressDTO' from content with media type 'application/json'.",
      "type": "System.Net.Http.UnsupportedMediaTypeException",
      "stacktrace": "   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\\\r\\\n   at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\\\r\\\n   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
    }
  }
}

Solution

  • I could not get it to work using an Odata controller, and I had to get past it, so what I ended up doing was creating a regular API controller which worked fine.

    public IHttpActionResult SetCoordinatesOnAddressList(IEnumerable<Address> addresses)
    {
        //Do stuff
    }
    

    This was able to accept an Address IEnumerable without any setup and it just worked.