IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| The Options Pattern in .NET Core | ASP.NET Core Web API Setup | |
REST Principles and API Design in .NET Core |
REST is a foundational architectural style for designing networked applications, especially web APIs. Here's a breakdown of its core principles and how they influence effective API design:
REST is built on a few key constraints that guide how resources are accessed and manipulated:
Designing a RESTful API means adhering to these principles while ensuring usability and maintainability:
Represent resources, not actions.
Example: /users instead of /getUsers
| Method | Action | Example |
|---|---|---|
| GET | Read | /users/123 |
| POST | Create | /users |
| PUT | Update | /users/123 |
| DELETE | Remove | /users/123 |
Example: /products, /orders, /users
Example: /api/v1/users
| Code | Meaning |
|---|---|
| 200 | OK |
| 201 | Created |
| 400 | Bad Request |
| 401 | Unauthorized |
| 404 | Not Found |
| 500 | Internal Server Error |
Example: /users?role=admin&sort=name&page=2
Letβs build a basic API for managing Products.
csharp
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
csharp
public interface IProductRepository
{
IEnumerable GetAll();
Product GetById(int id);
void Add(Product product);
void Update(Product product);
void Delete(int id);
}
csharp
[ApiController]
[Route("api/v1/products")]
public class ProductsController : ControllerBase
{
private readonly IProductRepository _repository;
public ProductsController(IProductRepository repository)
{
_repository = repository;
}
[HttpGet]
public IActionResult GetAll() => Ok(_repository.GetAll());
[HttpGet("{id}")]
public IActionResult GetById(int id)
{
var product = _repository.GetById(id);
return product == null ? NotFound() : Ok(product);
}
[HttpPost]
public IActionResult Create(Product product)
{
_repository.Add(product);
return CreatedAtAction(nameof(GetById), new { id = product.Id }, product);
}
[HttpPut("{id}")]
public IActionResult Update(int id, Product product)
{
if (id != product.Id) return BadRequest();
_repository.Update(product);
return NoContent();
}
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
_repository.Delete(id);
return NoContent();
}
}
/api/v1/...) to support future changes.[Required], [Range], etc.| Advantage | Description |
|---|---|
| π Platform Independent | Works across browsers, mobile apps, and servers |
| π Stateless | Simplifies server design and improves scalability |
| π§© Modular | Easy to extend and version |
| π§ Intuitive | Uses standard HTTP methods and status codes |
| π§° Tooling Support | Rich ecosystem for testing, monitoring, and documentation |
| Disadvantage | Description |
|---|---|
| π¦ Overhead | Repeated data in stateless requests adds extra payload |
| π§ No Built-in Query Language | Complex queries need custom design |
| π Tight Coupling | Structure changes can break existing clients |
| π Performance | Less efficient than gRPC for high-throughput scenarios |
| π§΅ No Real-Time Support | Not ideal for push-based real-time updates |
| The Options Pattern in .NET Core | ASP.NET Core Web API Setup | |