Web Api Optional Parameters in the middle with attribute routing
Optional parameters must be at the end of the route template. so what you are trying to do is not possible.
Attribute routing: Optional URI Parameters and Default Values
you either change your route template
[Route("Calls/{id:int?}/{callId:int?}")]
or create a new action
[RoutePrefix("api/Employees")]
public class CallsController : ApiController {
//GET api/Employees/1/Calls
//GET api/Employees/1/Calls/1
[HttpGet]
[Route("{id:int}/Calls/{callId:int?}")]
public async Task<ApiResponse<object>> GetCall(int id, int? callId = null) {
var testRetrieve = id;
var testRetrieve2 = callId;
throw new NotImplementedException();
}
//GET api/Employees/Calls
[HttpGet]
[Route("Calls")]
public async Task<ApiResponse<object>> GetAllCalls() {
throw new NotImplementedException();
}
}
Actually you dont need to specify optional parameter in route
[Route("Calls")]
or you need to change the route
[Route("Calls/{id:int?}/{callId:int?}")]
public async Task<ApiResponse<object>> GetCall(int? id = null, int? callId = null)
I would change the Route to:
[Route("Calls/{id:int?}/{callId:int?}")]
and add the [FromUri]
attribute to your parameters:
([FromUri]int? id = null, [FromUri]int? callId = null)
My test function looks like this:
[HttpGet]
[Route("Calls/{id:int?}/{callId:int?}")]
public async Task<IHttpActionResult> GetCall([FromUri]int? id = null, [FromUri]int? callId = null)
{
var test = string.Format("id: {0} callid: {1}", id, callId);
return Ok(test);
}
I can invoke it using:
https://localhost/WebApplication1/api/Employees/Calls
https://localhost/WebApplication1/api/Employees/Calls?id=3
https://localhost/WebApplication1/api/Employees/Calls?callid=2
https://localhost/WebApplication1/api/Employees/Calls?id=3&callid=2