0

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?

1 Answer 1

4

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)   {

  }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Damn it, I knew it was simple....decorate the method.....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.