How to have more than one GET request?

Asked

Viewed 867 times

0

I want to be able to fetch my items by name and this function is also a get request only it already has other requests Get wanted to understand how to get as many as I want

I understand the program doesn’t know which one to go to

of that mistake here:

An unhandled Exception occurred while Processing the request. Ambiguousmatchexception: The request Matched Multiple endpoints. Matches:

challenge.Controllers.Itemcontroller.Get (challenge) challenge.Controllers.Itemcontroller.Search (challenge) Microsoft.AspNetCore.Routing.Matching.Defaultendpointselector.Reportambiguity(Candidateset candidates)

    [HttpGet]
    public ActionResult<List<Item>> Get()
    {
        return _itemService.Get();
    }

    [HttpGet("{id}", Name = "GetItem")]
    public ActionResult<Item> Get(string id)
    {
        var item = _itemService.Get(id);
        if (item == null)
        {
            return NotFound();
        }

        return item;
    }

    [HttpGet("{name}")]
    public ActionResult<List<Item>> Buscar(string name)
    {
        var item = _itemService.Buscar(keyword);
        if(item == null)
        {
            return NotFound();
        }

        return _itemService.Get();
    }

1 answer

0


just name the different path, ex:

[HttpGet("Buscar/{name}")]  // seu get vai controller/buscar/name
    public ActionResult<List<Item>> Buscar(string name)
    {
        var item = _itemService.Buscar(keyword);
        if(item == null)
        {
            return NotFound();
        }

        return _itemService.Get();
    }
  • 1

    Thank you very much Lucas, I was doing similar but did not give the result I expected, you helped me a lot, thank you!!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.