'How to make where query in HttpGet request in .net core web api
I'm trying to make a HttpGet request in my ASP.NET Core Web API.
The problem is that I don't know how to make get request with query.
This is my model:
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; }
}
Controller
// 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 is an id from another table in the SQL database, and I need to select all rows where id = x.
For example idZamestnanca = 9 appears in the table 10 times and I need the query to return just these 10 rows.
When I try to make request with idZamestnanca, I get a status 404.
Solution 1:[1]
"When I try to make request with idZamestnanca, I get a status 404.":
There may two major reason for your
404you are getting now.
When Controller Hits But Returns with 404 :
Let's imagine this is your table. You would like to search by
idZamestnancafrom this table. HerePrimaryKeyisId. But you would like to search byidZamestnancain that caseFindAsync(idZamestnanca)will not work. BecauseFindAsync"finds an entity with the given primary key values"soFindAsyncwill always return404
Solution For Your Scenario :
Instead of that you should use below code to execute your query:
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
ListBut you have definedsingle objectTask<ActionResult<Predoslepozicie>>here.
Final Fixing :
Replace either Task<ActionResult<List<Predoslepozicie>>> this so your Controller Action should be look like below:
[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;
}
Or you can simply do like below:
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 routingcorrectly. When you set yourRoute Attributeas like[HttpGet("{idZamestnanca}")]in that scenario you have to call yourAPI URLlike below:
https://localhost:7132/api/YourControllerName/101 // Need to call like this
Note:Remember that here you have to pass your parameter directly aftercontrollerfollowed by/backslashnot by?or like below
https://localhost:7132/api/YourControllerName?idZamestnanca=1 // Not like this
Hope above explanation and guideline help you to resolve your problem completely.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |



