I am getting an error because I added a FromQuery parameter for a Get statement, to return a list, when I already had a Get statement using routing for the detail. Has anybody run into this issue of routing (for detail) vs additional parameters in a standard Get?
I have one api to get detail about one case:
[HttpGet("{id}")]
public async Task<ActionResult<CaseDTO>> Get(string guid)
{
...
}
and I had an api to get a list of cases:
[HttpGet]
public async Task<ActionResult<CaseListDTO>> Get()
{
...
}
all worked great but I wanted to add a parameter to the list function so I could offer an opportunity for additional fields to be returned if necessary:
[HttpGet]
public async Task<ActionResult<CaseListDTO>> Get([FromQuery]string mode = "")
{
...
}
Has anybody else run into this and is there a best practice for handling this situation?
UPDATE
I didn't completely solve this yet but I did get a part of it.
Here is what I did:
[Route("{caseId:int}")]
public async Task<ActionResult<OutboundCaseDTO>> Get(int caseId)
I have this at the controller/class level:
[Route("api/v{version:apiVersion}/[controller]")]
And when I ran into a problem with RoutePrefixAttribute, this so post helped.
I want to have 3 Get methods:
- List all cases
- List cases with additional attributes
- Get case detail using a guid
- Get case detail using an id
Right now I have 1, 3, and 4.
Working through the documentation provided helped, but there was a reference to RoutePrefix which got me sidetracked, but I expect I will need to come back to it as this evolves.
MORE DISCOVERY
I was getting an error when running:
The constraint reference 'string' could not be resolved to a type. Register the constraint type with 'Microsoft.AspNetCore.Routing.RouteOptions.ConstraintMap'
Which led me to this and I started looking into Rout
vs HttpGet
and found this helpful.
This routing documentation really helped based on this post.