[[oktatas:programozas:csharp:dotnetcore:web_api|< Web API]]
====== .Net Core Web API készítés ======
* **Szerző:** Sallai András
* Copyright (c) Sallai András, 2022
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]]
* Web: https://szit.hu
===== Dolgozók tárolása =====
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; }
}
}
===== Kontroller =====
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>> Get()
{
var employees = new List
{
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/
* Kattintok: GET /api/Employee
* Kattintok: Try it out
* Kattintok: Execute
===== Az employees külön =====
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace App01.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
private static List employees = new List
{
new Employee{
Id=1,
Name="Per Ede",
City="Szeged",
Salary=2830000
}
};
[HttpGet]
public async Task>> Get()
{
return Ok(employees);
}
}
}
===== Post metódus =====
[HttpPost]
public async Task>> AddEmployee(Employee employee)
{
employees.Add(employee);
return Ok(employees);
}
===== API teljes =====
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace App01.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
private static List employees = new List
{
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>> Get()
{
return Ok(employees);
}
[HttpGet("{id}")]
public async Task>> 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>> AddEmployee(Employee employee)
{
employees.Add(employee);
return Ok(employees);
}
[HttpPut]
public async Task>> 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>> 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);
}
}
}