Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# API通过字符串获取_C#_Postgresql_Api_Get_Asp.net Core Mvc - Fatal编程技术网

C# API通过字符串获取

C# API通过字符串获取,c#,postgresql,api,get,asp.net-core-mvc,C#,Postgresql,Api,Get,Asp.net Core Mvc,当一个单词匹配一个表字段时,我需要从数据库中获取所有数据。 我尝试在http方向(GetRutaByText)中使用一个参数,如下所示 ../api/ruta/GetRutaByText/'var'和 ../api/ruta/GetRutaByText/var 两者都返回空,不是错误,而是空。 我还尝试从body(GetRutaByText2)发送“var”文本 间期 Task<IEnumerable<Ruta>> GetRutasByText(string v

当一个单词匹配一个表字段时,我需要从数据库中获取所有数据。 我尝试在http方向(GetRutaByText)中使用一个参数,如下所示
../api/ruta/GetRutaByText/'var'
../api/ruta/GetRutaByText/var
两者都返回空,不是错误,而是空。 我还尝试从body(GetRutaByText2)发送“var”文本

间期


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


任务GetRutasByText(字符串值);
任务GetRutasByText2(Ruta-Ruta);
存储库:


    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 });
            }


公共异步任务GetRutasByText(字符串值)
{
var db=dbConnection();
var sql=@“选择*
来自public.tb_ruta
其中descripp i类似“%@Value%”;
返回wait db.QueryAsync(sql,new{Value=Value});
}
公共异步任务GetRutasByText2(Ruta Ruta)
{
var db=dbConnection();
var sql=@“选择*
来自public.tb_ruta
其中descripp i类似“%@descripp%”;
返回wait db.QueryAsync(sql,new{ruta.descripp});
}
控制器:


    [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));
            }

[Route("api/[controller]")]
    [ApiController]
    public class RutaController : ControllerBase
    {
        [HttpGet("GetRutaByText/{value}")]
        public async Task<IActionResult> GetRutaByText(string value)
        {
            return Ok(value);
        }

        

    }

[HttpGet(“{value}”)]
公共异步任务GetRutaByText(字符串值)
{
返回Ok(wait_rutaRepository.GetRutasByText(value));
}
[HttpGet]
公共异步任务GetRutaByText2([FromBody]Ruta Ruta)
{
返回Ok(wait_rutaRepository.GetRutasByText2(ruta));
}
这是邮差测试的结果


首先,您需要在
sql
中将
ilike
更改为
like
。然后您需要调试并检查您的操作是否获得了值。下面是一个在api中获得值的演示

控制器:


    [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));
            }

[Route("api/[controller]")]
    [ApiController]
    public class RutaController : ControllerBase
    {
        [HttpGet("GetRutaByText/{value}")]
        public async Task<IActionResult> GetRutaByText(string value)
        {
            return Ok(value);
        }

        

    }

Yiyi You使用的解决方案:

如下所示:

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

        }
公共异步任务GetRutasByText(字符串值)
{
var db=dbConnection();
返回wait db.QueryAsync(“select*from public.tb_ruta,其中descripp ilike“%”+value+“%”,new{value=value});
}
而不是:

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 });

        }
公共异步任务GetRutasByText(字符串值)
{
var db=dbConnection();
var sql=@“选择*
来自public.tb_ruta
其中descripp i类似“%@Value%”;
返回wait db.QueryAsync(sql,new{Value=Value});
}

我使用ilike是因为我需要它忽略大写或小写。我知道like和%,的用法,我所寻找的是正确的,在任何位置都有var,Ruta中有几个条目有“var”。我将检查[HttpGet(“GetRutaByText/{value}”)],我想我的错误可能在这里。感谢您选中了,没有,它不起作用,而且在您的示例中显示了200 OK,我的示例也显示了200 OK,只是如果您尝试使用
return wait db.querySync(“select*from public.tb_ruta where descripp ilike%”+value+“%”),它不会返回任何值,它能起作用吗?可能是“在
%@Value%
之外”导致了结果。嘿,是的,起作用了!!!如果我的回答有帮助,你们能把它标记为答案吗?谢谢。