-- React - asp - api --- 1. dotnet new webapi -n AspApi 2. Cài đặt các package dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Tools 3. Tạo model Product namespace AspApi.Models { public class Product { public int Id { get; set; } public string Name { get; set; } = string.Empty; public decimal Price { get; set; } public string Img { get; set; } = string.Empty; } } 4. Tao DBContext using Microsoft.EntityFrameworkCore; using AspApi.Models; namespace AspApi.Data { public class AppDbContext : DbContext { public AppDbContext(DbContextOptions options) : base(options) { } public DbSet Products => Set(); } } 5. Cấu hình trong appSetting.json { "ConnectionStrings": { "DefaultConnection": "Server=localhost;Database=ProductDB;Trusted_Connection=True;TrustServerCertificate=True;" }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*" } 6. Cấu hình EF và load DbContext vào program.cs using Microsoft.EntityFrameworkCore; using AspApi.Data; var builder = WebApplication.CreateBuilder(args); // Add DbContext builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))); builder.Services.AddControllers(); var app = builder.Build(); app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run(); 7. Tạo CRUD cho Controller using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using AspApi.Data; using AspApi.Models; namespace AspApi.Controllers { [Route("api/[controller]")] [ApiController] public class ProductsController : ControllerBase { private readonly AppDbContext _context; public ProductsController(AppDbContext context) { _context = context; } // GET: api/products [HttpGet] public async Task>> GetProducts() { return await _context.Products.ToListAsync(); } // GET: api/products/5 [HttpGet("{id}")] public async Task> GetProduct(int id) { var product = await _context.Products.FindAsync(id); if (product == null) return NotFound(); return product; } // POST: api/products [HttpPost] public async Task> PostProduct(Product product) { _context.Products.Add(product); await _context.SaveChangesAsync(); return CreatedAtAction(nameof(GetProduct), new { id = product.Id }, product); } // PUT: api/products/5 [HttpPut("{id}")] public async Task PutProduct(int id, Product product) { if (id != product.Id) return BadRequest(); _context.Entry(product).State = EntityState.Modified; await _context.SaveChangesAsync(); return NoContent(); } // DELETE: api/products/5 [HttpDelete("{id}")] public async Task DeleteProduct(int id) { var product = await _context.Products.FindAsync(id); if (product == null) return NotFound(); _context.Products.Remove(product); await _context.SaveChangesAsync(); return NoContent(); } } } 8. Tạo Database - Migration dotnet ef migrations add InitialCreate # Tạo ra thư mục Migrations và nội dung dotnet ef database update #ket noi db va tạo các bảng từ migrations 9. Chạy thử API dotnet run 10. Test api bằng postman GET /api/products Lấy danh sách sản phẩm GET /api/products/{id} Lấy 1 sản phẩm POST /api/products Thêm sản phẩm PUT /api/products/{id} Cập nhật sản phẩm DELETE /api/products/{id} Xóa sản phẩm 11. Thêm lệnh trong program để có thể lấy từ react (host khác) //Mo CORS builder.Services.AddCors(options => { options.AddPolicy("AllowAll", b => b.AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod()); }); var app = builder.Build(); app.UseCors("AllowAll");//- 12. Thực thi kết quả từ react?