2021年5月3日星期一

API Get by string

I need to get all data from db when a word matches a table field. I've tried using a parameter in the http direction (GetRutaByText), like this .../api/ruta/GetRutaByText/'var' and .../api/ruta/GetRutaByText/var both return empty, not error, but empty. I also tried sending the "var" text from body (GetRutaByText2)

Interphase

      Task<IEnumerable<Ruta>> GetRutasByText(string value);            Task<IEnumerable<Ruta>> GetRutasByText2(Ruta ruta);    

Repository:

      public async Task<IEnumerable<Ruta>> GetRutasByText(string value)              {                  var db = dbConnection();                  var sql = @"select *                              from public.tb_ruta                              where descrip ilike '%@Value%'";                  return await db.QueryAsync<Ruta>(sql, new { Value = value });                    }                    public async Task<IEnumerable<Ruta>> GetRutasByText2(Ruta ruta)              {                  var db = dbConnection();                  var sql = @"select *                              from public.tb_ruta                              where descrip ilike '%@descrip%'";                  return await db.QueryAsync<Ruta>(sql, new { ruta.descrip });              }    

Controller:

      [HttpGet("{value}")]              public async Task<IActionResult> GetRutaByText(string value)               {                  return Ok(await _rutaRepository.GetRutasByText(value));              }                    [HttpGet]              public async Task<IActionResult> GetRutaByText2([FromBody] Ruta ruta)               {                  return Ok(await _rutaRepository.GetRutasByText2(ruta));              }    

This are the results of the postman test 1

2

3

https://stackoverflow.com/questions/67377071/api-get-by-string May 04, 2021 at 07:43AM

没有评论:

发表评论