如何在 .net core web 中的 HttpGet 请求中进行 where 查询 api
How to make where query in HttpGet request in .net core web api
我正在尝试在我的 ASP.NET 核心网站 API 中发出 HttpGet
请求。
问题是我不知道如何使用查询进行获取请求。
这是我的模型:
public class Predoslepozicie
{
[Key]
public int Id { get; set; }
[Required]
public int idZamestnanca { get; set; }
[Required]
public string Pozicia { get; set; }
[Required]
public DateTime DatumUkoncenia { get; set; }
[Required]
public DateTime DatumNastupu { get; set; }
}
控制器
// GET: api/Predoslepozicie/5
[HttpGet("{idZamestnanca}")]
public async Task<ActionResult<Predoslepozicie>> GetPredoslepozicie(int idZamestnanca)
{
var Predoslepozicie = await _context.Predoslepozicie.FindAsync(idZamestnanca);
if (Predoslepozicie == null)
{
return NotFound();
}
return Predoslepozicie;
}
idZamestnanca
是 SQL 数据库中另一个 table 的 ID,我需要 select 所有行 where id = x
.
例如 idZamestnanca = 9
在 table 中出现了 10 次,我需要查询 return 这 10 行。
当我尝试使用 idZamestnanca
发出请求时,我收到状态 404。
"When I try to make request with idZamestnanca, I get a status 404.":
There may two major reason for your 404
you are getting now.
When Controller Hits But Returns with 404 :
Let's imagine this is your table. You would like to search by
idZamestnanca
from this table. Here PrimaryKey
is Id
. But you
would like to search by idZamestnanca
in that case
FindAsync(idZamestnanca)
will not work. Because FindAsync
"finds an entity with the given primary key values"
so FindAsync
will always return 404
Solution For Your Scenario :
您应该使用以下代码来执行您的查询:
var Predoslepozicie = _context.Predoslepozicies.Where(id=>id.idZamestnanca == idZamestnanca).ToList();
Note:
When you would replace above query then it would yell at you.
Because this a List
But you have defined single object
Task<ActionResult<Predoslepozicie>>
here.
Final Fixing :
替换 Task<ActionResult<List<Predoslepozicie>>>
这样你的 Controller Action
应该如下所示:
[HttpGet("{idZamestnanca}")]
public async Task<ActionResult<List<Predoslepozicie>>> GetPredoslepozicie(int idZamestnanca)
{
var Predoslepozicie = _context.Predoslepozicies.Where(id => id.idZamestnanca == idZamestnanca).ToList();
if (Predoslepozicie == null)
{
return NotFound();
}
return Predoslepozicie;
}
或者你可以简单地像下面这样:
Good Practice :
[Route("api/[controller]")]
[ApiController]
public class FrodoNicitelController : ControllerBase
{
private readonly ApplicationDbContext _context;
public FrodoNicitelController(ApplicationDbContext context)
{
_context = context;
}
[HttpGet("{idZamestnanca}")]
public async Task<ActionResult> GetPredoslepozicie(int idZamestnanca)
{
var Predoslepozicie = _context.Predoslepozicies.Where(id => id.idZamestnanca == idZamestnanca).ToList();
if (Predoslepozicie == null)
{
return NotFound();
}
return Ok(Predoslepozicie);
}
}
Output :
When Controller Does't Hits and Returns with 404 :
In this case you are not sending your Attribute routing
correctly. When you set your Route Attribute
as like
[HttpGet("{idZamestnanca}")]
in that scenario you have to call your
API URL
like below:
https://localhost:7132/api/YourControllerName/101 // Need to call like this
Note:
Remember that here you have to pass your parameter directly after controller
followed by /
backslash
not by ?
or
like below
https://localhost:7132/api/YourControllerName?idZamestnanca=1 // Not like this
希望以上解释和指南能帮助您彻底解决问题。
我正在尝试在我的 ASP.NET 核心网站 API 中发出 HttpGet
请求。
问题是我不知道如何使用查询进行获取请求。
这是我的模型:
public class Predoslepozicie
{
[Key]
public int Id { get; set; }
[Required]
public int idZamestnanca { get; set; }
[Required]
public string Pozicia { get; set; }
[Required]
public DateTime DatumUkoncenia { get; set; }
[Required]
public DateTime DatumNastupu { get; set; }
}
控制器
// GET: api/Predoslepozicie/5
[HttpGet("{idZamestnanca}")]
public async Task<ActionResult<Predoslepozicie>> GetPredoslepozicie(int idZamestnanca)
{
var Predoslepozicie = await _context.Predoslepozicie.FindAsync(idZamestnanca);
if (Predoslepozicie == null)
{
return NotFound();
}
return Predoslepozicie;
}
idZamestnanca
是 SQL 数据库中另一个 table 的 ID,我需要 select 所有行 where id = x
.
例如 idZamestnanca = 9
在 table 中出现了 10 次,我需要查询 return 这 10 行。
当我尝试使用 idZamestnanca
发出请求时,我收到状态 404。
"When I try to make request with idZamestnanca, I get a status 404.":
There may two major reason for your
404
you are getting now.
When Controller Hits But Returns with 404 :
Let's imagine this is your table. You would like to search by
idZamestnanca
from this table. HerePrimaryKey
isId
. But you would like to search byidZamestnanca
in that caseFindAsync(idZamestnanca)
will not work. BecauseFindAsync
"finds an entity with the given primary key values"
soFindAsync
will always return404
Solution For Your Scenario :
您应该使用以下代码来执行您的查询:
var Predoslepozicie = _context.Predoslepozicies.Where(id=>id.idZamestnanca == idZamestnanca).ToList();
Note:
When you would replace above query then it would yell at you.Because this a
List
But you have definedsingle object
Task<ActionResult<Predoslepozicie>>
here.
Final Fixing :
替换 Task<ActionResult<List<Predoslepozicie>>>
这样你的 Controller Action
应该如下所示:
[HttpGet("{idZamestnanca}")]
public async Task<ActionResult<List<Predoslepozicie>>> GetPredoslepozicie(int idZamestnanca)
{
var Predoslepozicie = _context.Predoslepozicies.Where(id => id.idZamestnanca == idZamestnanca).ToList();
if (Predoslepozicie == null)
{
return NotFound();
}
return Predoslepozicie;
}
或者你可以简单地像下面这样:
Good Practice :
[Route("api/[controller]")]
[ApiController]
public class FrodoNicitelController : ControllerBase
{
private readonly ApplicationDbContext _context;
public FrodoNicitelController(ApplicationDbContext context)
{
_context = context;
}
[HttpGet("{idZamestnanca}")]
public async Task<ActionResult> GetPredoslepozicie(int idZamestnanca)
{
var Predoslepozicie = _context.Predoslepozicies.Where(id => id.idZamestnanca == idZamestnanca).ToList();
if (Predoslepozicie == null)
{
return NotFound();
}
return Ok(Predoslepozicie);
}
}
Output :
When Controller Does't Hits and Returns with 404 :
In this case you are not sending your
Attribute routing
correctly. When you set yourRoute Attribute
as like[HttpGet("{idZamestnanca}")]
in that scenario you have to call yourAPI URL
like below:
https://localhost:7132/api/YourControllerName/101 // Need to call like this
Note:
Remember that here you have to pass your parameter directly aftercontroller
followed by/
backslash
not by?
or like below
https://localhost:7132/api/YourControllerName?idZamestnanca=1 // Not like this
希望以上解释和指南能帮助您彻底解决问题。