0

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:

  1. List all cases
  2. List cases with additional attributes
  3. Get case detail using a guid
  4. 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.

lcj
  • 1,355
  • 16
  • 37

1 Answers1

1

What you need is routing. Because you have two methods with same name (but different signature) the application routing is unable to figure out which Get it should be calling. You can overcome this by specifying routing. That way, depending on the kind of URL you pass on (i.e. kind of resource you are requesting) it forwards the method to appropriate Get method.

Read more about routing here - https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

This is a potential duplicate of - Single controller with multiple GET methods in ASP.NET Web API

Amogh Sarpotdar
  • 544
  • 4
  • 15
  • I'm working through this now. I think the first link is more in line with what I'm working for as Adding Route Attributes might be the right way to go. I added an update to the post based on your feedback. – lcj Jul 04 '22 at 12:06