Buổi 2 – HTTP, REST API & Routing

1. Lý thuyết (1 giờ)

1.1. HTTP cơ bản

  • HTTP là gì? (HyperText Transfer Protocol) – giao thức giao tiếp giữa client và server.
    • Request – Response cycle: Client gửi request → Server xử lý → Trả về response.
    • HTTP methods (CRUD mapping):
    • GET – Lấy dữ liệu.
    • POST – Thêm mới dữ liệu.
    • PUT / PATCH – Cập nhật dữ liệu.
    • DELETE – Xoá dữ liệu.
  • HTTP Status Codes (phân loại chính):
    • 1xx – Informational.
    • 2xx – Success (200 OK, 201 Created).
    • 3xx – Redirect.
    • 4xx – Client errors (400 Bad Request, 404 Not Found, 401 Unauthorized).
    • 5xx – Server errors (500 Internal Server Error).

1.2. REST API

  • REST (Representational State Transfer) – nguyên tắc thiết kế API:
    • Stateless.
    • Resource-based.
    • Sử dụng URI để định danh tài nguyên.
    • HTTP methods tương ứng với CRUD.
  • Ví dụ URI chuẩn REST:
    • /api/products – GET (lấy danh sách sản phẩm).
    • /api/products/{id} – GET (lấy 1 sản phẩm).
    • /api/products – POST (thêm sản phẩm).
    • /api/products/{id} – PUT/PATCH (cập nhật sản phẩm).
    • /api/products/{id} – DELETE (xóa sản phẩm).

1.3. Routing trong ASP.NET Core

  • Routing là gì? – cơ chế ánh xạ URL đến action trong controller.
  • Types of routing:

Conventional routing (định nghĩa trong Program.cs hoặc Startup.cs):

app.MapControllerRoute(
   name: "default",
   pattern: "{controller=Home}/{action=Index}/{id?}");
  • Attribute routing (đặt trực tiếp trên controller/action):
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
   [HttpGet]
   public IActionResult GetAll() => Ok("List of products");
   [HttpGet("{id}")]
   public IActionResult GetById(int id) => Ok($"Product {id}");
}
  • So sánh Conventional vs Attribute routing:
    • Conventional: Dễ quản lý khi dự án lớn.
    • Attribute: Linh hoạt, phù hợp cho API.

2. Thực hành (3 giờ)

  • Bài tập 1 – Tạo project API
    • Bước 1: Mở Visual Studio / VS Code, tạo ASP.NET Core Web API project.
    • Bước 2: Cấu hình launchSettings.json để chạy trên port cố định (VD: https://localhost:5001).
  • Bài tập 2 – Tạo API GET
    • Tạo ProductsController với API:
    • GET /api/products → Trả danh sách sản phẩm (mock data).
    • GET /api/products/{id} → Trả 1 sản phẩm theo id.
  • Bài tập 3 – Tạo API POST
    • API: POST /api/products → Nhận dữ liệu từ body (JSON), trả lại sản phẩm vừa thêm.
    • Dùng model Product:
public class Product
{
   public int Id { get; set; }
   public string Name { get; set; }
   public decimal Price { get; set; }
}
  • Bài tập 4 – Test API bằng Postman
    • Test GET, POST.
    • Kiểm tra status code, response body.
    • Thử gửi dữ liệu sai để xem lỗi trả về.

3. Kết quả mong đợi sau buổi học

  • Sinh viên hiểu rõ HTTP methods và status codes.
  • Biết cách tạo và test REST API cơ bản trong ASP.NET Core.
  • Nắm được 2 kiểu routing và khi nào dùng mỗi kiểu.
  • Có thể tự viết và test API CRUD đơn giản.

Bài viết liên quan

Buổi 1 – Giới thiệu & Cài đặt môi trường

15-08-2025 Admin
6 views + likes

Kiến trúc back-end & mô hình client–server. ASP.NET Core là gì...