I'm migrating a full .NET Framework Web API 2 REST project over to ASP.NET Core 2.2 and getting a bit lost in the routing.
In Web API 2 I was able to overload routes with the same number of parameters based on the parameter type, e.g. I could have Customer.Get(int ContactId)
and Customer.Get(DateTime includeCustomersCreatedSince)
and incoming requests would be routed accordingly.
I haven't been able to achieve the same thing in .NET Core, I either get a 405 error or a 404 and this error instead:
"{\"error\":\"The request matched multiple endpoints. Matches: \r\n\r\n[AssemblyName].Controllers.CustomerController.Get ([AssemblyName])\r\n[AssemblyName].Controllers.CustomerController.Get ([AssemblyName])\"}"
This was working code in my full .NET framework app Web API 2 app:
[RequireHttps]
public class CustomerController : ApiController
{
[HttpGet]
[ResponseType(typeof(CustomerForWeb))]
public async Task<IHttpActionResult> Get(int contactId)
{
// some code
}
[HttpGet]
[ResponseType(typeof(List<CustomerForWeb>))]
public async Task<IHttpActionResult> Get(DateTime includeCustomersCreatedSince)
{
// some other code
}
}
And this is what I converted it to in Core 2.2:
[Produces("application/json")]
[RequireHttps]
[Route("api/[controller]")]
[ApiController]
public class CustomerController : Controller
{
public async Task<ActionResult<CustomerForWeb>> Get([FromQuery] int contactId)
{
// some code
}
public async Task<ActionResult<List<CustomerForWeb>>> Get([FromQuery] DateTime includeCustomersCreatedSince)
{
// some code
}
}
The code above works if I comment out one of Get
methods, but fails as soon as I have two Get
methods. I'd expected the FromQuery
to use the parameter name in the request to steer the routing, but that doesn't seem to be the case?
Is it possible to overload a controller method like this where you have the same number of parameters and either route based on the parameter's type or the parameter's name?
Get(int companyId, int personId)
and you wanted to use just the personId, would you need to callCustomer/Get?personId=1234
? I.e. is the routing using the parameter's type or the parameter name to do the matching? – Baboon