Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Ưu điểm của C# ASP.NET WebAPI

WEB API là lựa chọn tốt hơn cho services đơn giản, lightweight. WEB API có thể dùng bất kỳ text format nào bao gồm XML và nhanh hơn WCF.

  • Hoạt động theo cách HTTP dùng standard HTTP verbs như GET, POST, PUT, DELETE cho tất cả CRUD operations.
  • Complete Support cho routing.
  • Response generated ở Json và XML format dùng MediaTypeFormatter.
  • Có khả năng host trong IIS cũng như self-host bên ngoài IIS.
  • Support Model binding và Validation.
  • Support cho ODATA.
  • Support stateless transfer của data.
  • Support Url patterns và Http methods.

Lưu ý - ODATA (Open Data Protocol) là open protocol cho phép tạo và consume queryable và interoperable RESTful APIs cách đơn giản và chuẩn.

Chi tiết các ưu điểm

1. HTTP-First Design

  • Tận dụng đầy đủ HTTP semantics: Verbs (GET/POST/PUT/PATCH/DELETE), Status Codes (200, 201, 400, 404, 500), Headers (Cache-Control, ETag, Content-Type)
  • Dễ dàng caching với ETag, Last-Modified
  • Hỗ trợ HATEOAS (Hypermedia as the Engine of Application State)

2. Flexible Hosting

Hosting Model Mô tả Use Case
IIS Hosting Host trong IIS, dùng ASP.NET pipeline Enterprise, production, Windows Server
Self-Host (HttpSelfHostServer) Host trong Console App, Windows Service, WPF Microservices, lightweight, cross-platform (ASP.NET Core)
OWIN/Katana Decouple từ IIS, middleware pipeline Transition to ASP.NET Core
ASP.NET Core (Kestrel) Cross-platform, high-performance Modern apps, Linux containers, Kubernetes

3. Content Negotiation & Formatters

  • Built-in: JSON (Json.NET), XML (XmlSerializer/DataContractSerializer), Form-Urlencoded
  • Extensible: Custom MediaTypeFormatter cho CSV, Protobuf, MessagePack, v.v.
  • Per-request format selection qua Accept header

4. Model Binding & Validation

  • Automatic binding từ Route, Query, Body, Header
  • Data Annotations validation ([Required], [StringLength], [Range], Custom)
  • IValidatableObject cho cross-property validation
  • ModelState.IsValid check trong action

5. OData Support

  • Query options: $filter, $orderby, $top, $skip, $expand, $select
  • EnableQueryAttribute cho IQueryable
  • OData v4 endpoint routing

6. Stateless & Scalable

  • No session affinity required
  • Horizontal scaling dễ dàng
  • Phù hợp microservices architecture

7. Modern ASP.NET Core Enhancements

  • Minimal APIs (less boilerplate)
  • Built-in DI, Configuration, Logging
  • OpenAPI/Swagger integration
  • gRPC support alongside REST
  • Performance: Kestrel, Pipelines, Span<T>

So sánh Web API vs WCF vs MVC

Feature ASP.NET Web API WCF ASP.NET MVC
Primary Purpose RESTful Services SOAP + REST Services Web Applications (HTML)
Protocol HTTP Only HTTP, TCP, MSMQ, Named Pipes HTTP
Serialization JSON, XML, Custom XML (DataContractSerializer) HTML, JSON (ActionResult)
Hosting IIS, Self-Host, OWIN IIS, WAS, Self-Host, Windows Service IIS
Complexity Low High Medium
Performance High Lower (SOAP overhead) Medium