Search code examples
c#.netget

GET action with multiple ids inputted .NET


It`s possible to create a GET action based on multiple id's inputted?

For example how can I change this method to be GetCustomer([FromRoute] int id, int code_id)?

// GET: api/Customer/5
[HttpGet("{id}")]
public async Task<IActionResult> GetCustomer([FromRoute] int id)
    {
      if (!ModelState.IsValid)
      {
         return BadRequest(ModelState);
      }

      var customerMaster = await _context.CustomerMaster.SingleOrDefaultAsync(m => m.Code == id);

      if (customerMaster == null)
      {
         return NotFound();
      }

      return Ok(customerMaster);
 }

Solution

  • Use AttributeRouting:

    // GET: api/Customer/5/3
    
    [Route("api/Customer/{id}/{code_id}")]
    public async Task<IActionResult> GetCustomer(int id, int code_id)
    {
    
            ...
       return Ok(customer);
    }