I have the following two methods defined in my WebAPI controller:
public class SocketController : ApiController
{
[HttpGet]
[Route("api/socket")]
public List<SocketInfo> GetAllSockets()
{
throw new Exception("Not Implemented; Use API/Socket/{ConfigId} to request a specific socket.");
}
[HttpGet]
[Route("api/socket/{Id}")]
public SocketInfo GetSocket(string configId)
{
SocketInfo si = new SocketInfo();
si.ConfigId = configId;
si.Password = "****************";
si.SystemName = "_SystemName";
si.Type = Definitions.SocketType.DTS;
si.Subtype = Definitions.SocketSubtype.PUT;
return si;
}
...
As expected, the url https://localhost:44382/API/Socket returns the exception:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>Not Implemented; Use API/Socket/{ConfigId} to request a specific socket.
</ExceptionMessage>
<ExceptionType>System.Exception</ExceptionType>
<StackTrace>
...
OK, let's try to retrieve a specific socket by Id: https://localhost:44382/API/Socket/ab24def6
But for some reason, this doesn't route. Here's what I get:
<Error>
<Message>No HTTP resource was found that matches the request URI
'https://localhost:44382/API/Socket/ab24def6'.
</Message>
<MessageDetail>No action was found on the controller 'Socket' that matches the request.
</MessageDetail>
</Error>
Does anyone have any idea why this is not routing?
Try this:
[HttpGet, Route("api/socket/{configId}")]
public SocketInfo GetSocket([FromRoute] string configId)
{
// ...
}
The problem was that your parameter name didn't match the route parameter name. [FromRoute]
is optional in this case, but it makes it clearer to the programmer where the data is to originate from.