Home > Back-end >  How to make where query in HttpGet request in .net core web api
How to make where query in HttpGet request in .net core web api

Time:04-23

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.

CodePudding user response:

"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 :

enter image description here

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 enter image description here

Because this a List But you have defined single object Task<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 :

enter image description here

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

Hope above explanation and guideline help you to resolve your problem completely.

  • Related