Search for several API ids. net core

Asked

Viewed 27 times

0

I do not know whether I was clear in the name however I have a doubt, I possess a route as follows:

/api/produto/v1/Aplicacao/filter

where this endpoint is a GET that passes a word as parameter and it searches the words that were passed.

The question is this, I have how to do the route something like /api/produto/v1/Aplicacao/filter/categoria/{id}, where the category id is optional and if I pass nothing in the category it uses only the route up /api/produto/v1/Aplicacao/filter and search the word that was passed by parameter?

1 answer

1


A solution would be to pass a category id along with the words on the route /api/produto/v1/Aplicacao/filter and, if this id is null, search only by words.

To do this, just create a DTO with these two fields: a word list and a category id. This DTO would be passed as a parameter in the request within the controller.

DTO

public class YourDto
{
    public List<String> Palavras { get; set; }
    public int CategoriaId { get; set; }
}

Controller

public IActionResult yourMethodName([FromBody] YourDto dto)

So just check if it’s null the ID and if it is, just do the word search.

Browser other questions tagged

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