2

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)"
    }
  }
}
4
  • just use user definded model like this...foo{inenumerable<Address> addresss} Commented Jun 29, 2015 at 13:58
  • and then your api controller action method should be SetCoordinatesOnAddressList(foo addressModel) Commented Jun 29, 2015 at 13:59
  • I tried but It didn't work. I edited the original question, with your suggestion. Please take a look. Commented Jun 30, 2015 at 7:36
  • try to addd request header... Content-Type: application/json Commented Jun 30, 2015 at 7:51

4 Answers 4

4

What happens if send the collection as a property named addresses on an object, like this?

{"addresses": [{"StreetName":"Some street","StreetNumber":"8","ZipCode":1234,"Town":"Some town"}]}
Sign up to request clarification or add additional context in comments.

1 Comment

The input parameter is still null, sadly.
3
[HttpPost]
[ODataRoute("SetCoordinatesOnAddressList")]
public IHttpActionResult SetCoordinatesOnAddressList(ODataActionParameters parameters)
{
    IEnumerable<Address> addresses =  parameters["addresses"] as IEnumerable<Address>;
    var result = addresses.Select(address => _coordinates.GetAddressCoordinates(address)).ToList();
    return Ok(result);
} 

WebAPIConfig should look like this

builder.Action("SetCoordinatesOnAddressList").Returns<IHttpActionResult>().CollectionParameter<Address>("addresses");

it is not bound to entity therefore access like this localhost/odata/SetCoordinatesOnAddressList

1 Comment

this should be an accepted answer - setting CollectionParameter did the job.
3

For action, why don't you use ODataActionParameter as the parameter of the method in the controller?

Here's a simple tutorial about how to use Action:

http://odata.github.io/WebApi/#04-07-action-parameter-support

In which, section "Entity and Collection of Entity parameter" is the same scenario, because I see you defined "Address" as entity type.

builder.EntityType()....

However, "Complex and Collection of Complex parameter" is the scenario for complex type.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.