I want two patterns of API url point to the same API action method:
api/Cities/{countryCode}
and
api/Cities
Is this possible to configure using Route
attribute?
I made this and didn't work:
[HttpGet, Route("GetCities/{code?}")]
public dynamic GetCities(string code)
{
return GENOrderRepository.SelectCities(Context, code);
}
Just create one action method, and use the route attribute like this:
Route[("api/Cities/{countryCode?}")]
(Note the question mark at the end, that makes a parameter optional). You also have to supply a default parameter to the parameter. See my working sample:
[HttpGet, Route("GetCities/{code?}")]
public IHttpActionResult GetCities(string code=null)
{
return Ok();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With