How to Set Up Multiple Web API Get

Asked

Viewed 1,580 times

4

I need to provide in my web api three forms of query: First Get with all "Getall" records, the second GET by id and a third GET with ribbon options, sent by client, e.g.: Name contains the letter "a" and address contains the word "bridge", I thought to send by queryString, but does not hit the correct action, incomplete example of how I thought to do the Get of filtered:

public HttpResponseMessage GetFiltrados() {
    string condicao = "";
    string[] filtros = Request.RequestUri.Query.Substring(1).Split('&');
    foreach (var filtro in filtros) {
        condicao += "";
    }
    if (reg != null) {
        return Request.CreateResponse(HttpStatusCode.OK, reg);
    } else {
        return Request.CreateResponse(HttpStatusCode.NotFound, "Registro não encontrado");
}

}

How do I create these three Get methods? I’m doing it right with Get with filters for REST default?
Ps.: My POST I did on the default, all in JSON, but GET didn’t know how to fix the ribbon.

  • Your question is not clear, you already have 2 Get’s and would like to do one more?

  • I edited the question, see if it got better

1 answer

7


I didn’t quite understand, but yours GET with the optional parameters can be this way:

[HttpGet,Route("api/Pessoas/ListarFiltrados")]
public IHttpActionResult ListarFiltrados(string? Nome= null, string? Sobrenome= null, int? idade= null)  
{
    var pessoas = db.Pessoas(Nome, Sobrenome, idade);
    return Ok(pessoas );
}

Notice that in front of each guy has the sign of ? which allows past attributes to be null.

The URL would look like this:

../api/ListarFiltrados?Nome=Diego&Sobrenome=Augusto&idade=23 

Another option would be to use Parameterbinding where you can pass an entire object to your Endpoint:

public IHttpActionResult ListarFiltrados([FromUri]Pessoa pessoa) {...}

Model Pessoa:

public class Pessoa
{
    public string Nome{ get; set; }
    public string Sobrenome{ get; set; }
    public int? Idade{ get; set; }
}

To queryString remains the same.

  • That’s almost what I need, as I will have several properties that I can send filters, can I receive everything in a single string and so mount dynamically? With the code below for example I can create an array of queryString and then try something dynamically, but I can’t hit the correct action, follow code: string[] filtros = Request.RequestUri.Query.Substring(1).Split('&');

  • If you have all this in an Object you can use parameter binding, I’ll add an example to the question

  • In the object Request.RequestUri.Querycomes exactly queryString but comes in wrong action

  • @Joãopaulo was unaware of this way you were using, I edited the answer see if it can help you with something.

  • It was a way that I found I don’t know if it’s better or if it’s correct, now I saw that it’s more complicated, see if you have any idea: I have a modal at the front end with a list, in each column the user can fill a filter that would be a "containing" in each column, this was the way I saw to receive the data from the filters, by querystring, if you have another form... Another difficulty is to assemble the Where dynamically EF, because it would normally do: .Where(a => a.Nome.Contains("a") && a.Endereco.Contains("ponte") But may or may not see these props and may be other

Browser other questions tagged

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