Reputation: 3399
I have a fairly simple controller for my API. One method returns all devices, and another method returns information on just one. Both of them are HttpGet.
My route definition:
[Route("api/device/list/{Id}")] [ApiController] public class DeviceController : ControllerBase
This method is always called when I pass in an ID:
[HttpGet]
public ActionResult<poiDevices> GetDeviceList()
The PostMan URL looks like this:
When the call above is made I want it to call this method below, defined as such with an Id parameter:
public ActionResult<DeviceDto> GetDeviceDetails( [FromRoute] string Id)
The code above has a placeholder for an Id, so my expectation is for this method to be called, instead the generic method is called which returns all. If I take the Id out of the URL the API returns 404 not found, meaning I messed up the routing piece of this.
What am I missing here?
Upvotes: 0
Views: 846
Reputation: 5085
You should change your controller like this.
[Route("api/device")]
[ApiController]
public class DeviceController : ControllerBase
{
[HttpGet]
[Route("list")]
public ActionResult<poiDevices> GetDeviceList() {
}
[Route("list/{id}")]
public ActionResult<DeviceDto> GetDeviceDetails(
[FromRoute] string Id) {
}
}
Upvotes: 4