A projekt alapkönyvtárában létrehozok egy Employee.cs fájlt:
namespace App01 { public class Employee { public int Id { get; set; } public string Name { get; set; } = string.Empty; public string City { get; set; } = string.Empty; public double Salary { get; set; } } }
Létrehozok egy kontrollert:
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace App01.Controllers { [Route("api/[controller]")] [ApiController] public class EmployeeController : ControllerBase { } }
Létrehozom benne a Get() metódust:
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace App01.Controllers { [Route("api/[controller]")] [ApiController] public class EmployeeController : ControllerBase { [HttpGet] public async Task<ActionResult<List<Employee>>> Get() { var employees = new List<Employee> { new Employee{ Id=1, Name="Per Ede", City="Szeged", Salary=2830000 } }; return Ok(employees); } } }
Elindítom a projektet, ha az még nem fut:
dotnet watch
A „dotnet run” is használható, de minden változtatáshoz újra kell indítani.
A watch is lebeszélhető az változás utáni újratöltésről:
dotnet watch --no-hot-reload
Swagger:
https://localhost:7040/swagger/
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace App01.Controllers { [Route("api/[controller]")] [ApiController] public class EmployeeController : ControllerBase { private static List<Employee> employees = new List<Employee> { new Employee{ Id=1, Name="Per Ede", City="Szeged", Salary=2830000 } }; [HttpGet] public async Task<ActionResult<List<Employee>>> Get() { return Ok(employees); } } }
[HttpPost] public async Task<ActionResult<List<Employee>>> AddEmployee(Employee employee) { employees.Add(employee); return Ok(employees); }
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace App01.Controllers { [Route("api/[controller]")] [ApiController] public class EmployeeController : ControllerBase { private static List<Employee> employees = new List<Employee> { new Employee{ Id=1, Name="Per Ede", City="Szeged", Salary=2830000 }, new Employee{ Id=2, Name="Pod Tamás", City="Szolnok", Salary=2549000 } }; [HttpGet] public async Task<ActionResult<List<Employee>>> Get() { return Ok(employees); } [HttpGet("{id}")] public async Task<ActionResult<List<Employee>>> Get(int id) { var employee = employees.Find(emp => emp.Id == id); if (employee == null) { return BadRequest("A dolgozó nem található"); } return Ok(employee); } [HttpPost] public async Task<ActionResult<List<Employee>>> AddEmployee(Employee employee) { employees.Add(employee); return Ok(employees); } [HttpPut] public async Task<ActionResult<List<Employee>>> UpdateEmployee(Employee request) { var employee = employees.Find(emp => emp.Id == request.Id); if (employee == null) { return BadRequest("A dolgozó nem található"); } employee.Name = request.Name; employee.City = request.City; employee.Salary = request.Salary; return Ok(employees); } [HttpDelete("{id}")] public async Task<ActionResult<List<Employee>>> Delete(int id) { var employee = employees.Find(emp => emp.Id == id); if (employee == null) { return BadRequest("A dolgozó nem található"); } employees.Remove(employee); return Ok(employee); } } }