I am creating an API the method below gets staff by their name in the query parameter or all staff. However, does the [FromQuery(Name = "name")]
protect from SQL injection, unsure if this is a default feature of .NET core 2.2?
[HttpGet]
public IActionResult GetStaff([FromQuery(Name = "name")] string firstName)
{
if (firstName == null)
{
//get all staff
var staff = _repo.GetAllStaff().ToList();
return Ok(staff);
}
if (firstName != null)
{
//get staff by firstName
var staffByName = _repo.GetStaffByName(firstName).ToList();
return Ok(staffByName);
}
return BadRequest("No staff found");
}
Method in Repository
public IEnumerable<ApiStaff> GetStaffByName(string name)
{
var staffName = _context.ApiStaff.Where(k => k.FirstName == name);
return staffName;
}