How to configure Web Api route to accept named parameter(query string)?

Asked

Viewed 1,098 times

2

I have a web api with the following method:

 [HttpGet]
 [Route("api/documento/doc/list?{cpf}")]
 public string Listar(string cpf)
 {
      return "value";
 } 

I need the above method to be called through the url:

http://localhost:54037/api/document/doc/list? Cpf=53724435886

The big problem is the "?" between the list and Cpf.

How can I change the route to work with the URL above?

  • The way you suggested I already tested it and unfortunately it does not answer me. This web api is for product integration and needs to have the URL: > http://localhost:54037/api/document/doc/list? Cpf=53724435886 The big question is precisely the list? Cpf.

  • let your route only so api/document/doc/list , that at the end qnd vc pass url api/document/doc/list? Cpf=53724435886 it will understand

  • Hi Jeferson, unfortunately it didn’t work. Curious is that even the route being [Route("api/document/doc/list")] the url that worked was: api/document/doc/? Cpf=53724435886.

1 answer

1


define the route without the "?" query string will be automatically mapped to the parameter of the same name.

 [HttpGet]
 [Route("api/documento/doc/list")]
 public string Listar(string cpf)
 {
      return "value";
 } 
  • Now it was!!! Thank you very much. :)

Browser other questions tagged

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