Egyszerű REST API készítés dotnet használatával, kontroller nélkül.
Telepítéshez Nuget csomagokat használunk:
dotnet new webapi -minimal -o app01
dotnet add package Pomelo.EntityFrameworkCore.MySql
app01/ |-bin |-database/ | `-create.sql |-obj/ |-Properties/ |-app01.csproj |-app01.sln |-appsettings.Development.json |-appsettings.json |-DataService.cs |-Emplyoee.cs `-Program.cs
dotnet watch run
create database if not exists sargabt; use sargabt; create table if not exists employees ( Id int not null primary key auto_increment, Name varchar(50), City varchar(50), Salary double ); grant all privileges on sargabt.* to sargabt@localhost identified by 'titok';
using Microsoft.EntityFrameworkCore; class DataService : DbContext { string str = "Server=localhost; User ID=sargabt; Password=titok; Database=sargabt"; public DbSet<Employee> Employees { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder ob) { ob.UseMySql(str, ServerVersion.AutoDetect(str)); } }
public class Employee { public int Id { get; set; } public string Name { get; set; } = ""; public string City { get; set; } = ""; public double Salary { get; set; } }
var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); var db = new DataService(); app.MapGet("/employees", () => { var emps = db.Employees.ToList(); return emps; }); app.MapPost("/employees", (Employee emp) => { db.Employees.Add(emp); db.SaveChanges(); return Results.Created(@"/employeeitems/{emp.Id}", emp); }); app.MapPut("/employees/{id}", (int id, Employee inputEmp) => { var emp = db.Employees.Find(id); if (emp is null ) return Results.NotFound(); emp.Name = inputEmp.Name; emp.City = inputEmp.City; emp.Salary = inputEmp.Salary; db.SaveChanges(); return Results.NoContent(); }); app.MapDelete("/employees/{id}", (int id) => { if(db.Employees.Find(id) is Employee emp) { db.Employees.Remove(emp); db.SaveChanges(); return Results.NoContent(); } return Results.NoContent(); }); app.Run();
Teles végpont példa:
Végpont | Metódus | Auth | Leírás |
---|---|---|---|
/employees | GET | nem | Az összes dolgozó lekérése |
/employees | POST | nem | Új dolgozó hozzáadása |
/employees/{id} | PUT | nem | Dolgozó adatainak frissítése |
/employees/{id} | DELETE | nem | Dolgozó törlése |
dotnet add package Swashbuckle.AspNetCore
var builder = WebApplication.CreateBuilder(args); var services = builder.Services; services.AddEndpointsApiExplorer(); services.AddSwaggerGen(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwagger(); app.MapGet("/msg", () => "működik"); app.UseSwaggerUI(); app.Run();
A „var app = builder.Build(); előtt:
var services = builder.Services; services.AddEndpointsApiExplorer(); services.AddSwaggerGen();
A „var db = new DataService();” után:
app.UseSwagger();
Az „app.Run();” előtt:
app.UseSwaggerUI();
var builder = WebApplication.CreateBuilder(args); var services = builder.Services; services.AddEndpointsApiExplorer(); services.AddSwaggerGen(); var app = builder.Build(); var db = new DataService(); app.UseSwagger(); app.MapGet("/employees", () => { var emps = db.Employees.ToList(); return emps; }); app.MapPost("/employees", (Employee emp) => { db.Employees.Add(emp); db.SaveChanges(); return Results.Created(@"/employeeitems/{emp.Id}", emp); }); app.MapPut("/employees/{id}", (int id, Employee inputEmp) => { var emp = db.Employees.Find(id); if (emp is null ) return Results.NotFound(); emp.Name = inputEmp.Name; emp.City = inputEmp.City; emp.Salary = inputEmp.Salary; db.SaveChanges(); return Results.NoContent(); }); app.MapDelete("/employees/{id}", (int id) => { if(db.Employees.Find(id) is Employee emp) { db.Employees.Remove(emp); db.SaveChanges(); return Results.NoContent(); } return Results.NoContent(); }); app.UseSwaggerUI(); app.Run();