Search code examples
c#odata

odata v4 - pass multiple parameters


I need to pass these parameters to an odata endpoint:

  • id
  • year
  • person [type of Person]

I tried with two parameters first, but it is not working. This is what I am getting:

The path template 'People({id}, {year})' on the action 'Get' in controller 'People' is not a valid OData path template. The number of keys specified in the URI does not match number of key properties for the resource

public class PeopleController : ODataController
    {

     [EnableQuery]
            [ODataRoute("People({id}, {year})")]
    
            public IHttpActionResult Get([FromODataUri] int id, [FromODataUri] int year)
            {
    
            }
    }



 public class Person
    {
        public int PersonId { get; set; }
           
        public string Email { get; set; }

        [Required]
        public string FirstName { get; set; }

        [Required]
        public string LastName { get; set; }
        [DataType(DataType.Date)]
        public DateTimeOffset DateOfBirth { get; set; }
        public Gender Gender { get; set; }
    }

How can I pass multiple parameters and especially these:

id : int
year : int
person : [type of Person]

Solution

  • You will have to create another DTO which will contain these properties:

    Eg:

    public class PersonInfo {
       public int id {get;set;}
       public year int {get;set;}
       public Person person {get;set;}
    }
    

    Then in your controller set it as follows:

     public IHttpActionResult Get(PersonInfo info)
     {
        //your gode here
     }