How can i perform routing with below URLs
I have following types of URLs
api/country
It will return all countryapi/country/1
It will return specific countryapi/country/1/state
it will return all state of country 1api/country/1/state/1
it will return specific state of specific countryapi/country/1/state/1/city
it will return all city of state 1 country 1api/country/1/state/1/city/1
it will return specific city of state 1 country 1Your route parameter does not have to say "id". You can pick any variable name. For instance:
[HttpGet("api/country/{countryId}/state/{stateId}/city/{cityId")]
public IActionResult GetCity(
[FromRoute]int countryId, [FromRoute]int stateId, [FromRoute]int cityId)
{
// pick what you want to return here
}
If you use the [ApiController]
on your controller class, you wouldn't have to use [FromRoute]
on every attribute.