Neo
Neo

Reputation: 3399

.Net Core 3.1 API Routing not working as expected

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:

https://localhost:5001/api/device/list/mycoolid

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

Answers (1)

Venkata Dorisala
Venkata Dorisala

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

Related Questions