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
| Middleware in ASP.NET Core | Filters in ASP.NET Core | |
HTTP Verbs in ASP.NET Core |
HTTP verbs (also known as HTTP methods) originated with the HTTP protocol in the early 1990s as part of the foundational architecture of the World Wide Web. Defined by the HTTP/1.0 and later HTTP/1.1 specifications, these verbs were designed to standardize how clients interact with resources on a server. Over time, they became central to RESTful architecture, which emphasizes stateless communication and resource-based design.
In ASP.NET Core Web API, HTTP verbs are mapped to controller actions using attributes like [HttpGet], [HttpPost], and others. This alignment with REST principles allows developers to build clean, scalable, and intuitive APIs that mirror real-world operations—such as retrieving, creating, updating, or deleting data.
| Verb | Purpose | Attribute | Idempotent | Safe | Example Method |
|---|---|---|---|---|---|
| GET | Retrieve data | [HttpGet] | Yes | Yes | GetBooks() |
| POST | Create a new resource | [HttpPost] | No | No | PostBook(Book book) |
| PUT | Replace an existing resource | [HttpPut] | Yes | No | PutBook(int id, Book book) |
| PATCH | Partially update a resource | [HttpPatch] | Yes | No | PatchBook(int id, JsonPatchDocument<Book> patch) |
| DELETE | Remove a resource | [HttpDelete] | Yes | No | DeleteBook(int id) |
[ApiController]
[Route("api/[controller]")]
public class BooksController : ControllerBase
{
private static List<string> books = new() { "Clean Code", "Domain-Driven Design" };
[HttpGet]
public IEnumerable<string> Get() => books;
[HttpPost]
public IActionResult Post([FromBody] string book)
{
books.Add(book);
return CreatedAtAction(nameof(Get), new { id = books.Count - 1 }, book);
}
[HttpPut("{id}")]
public IActionResult Put(int id, [FromBody] string book)
{
if (id >= books.Count) return NotFound();
books[id] = book;
return NoContent();
}
[HttpPatch("{id}")]
public IActionResult Patch(int id, [FromBody] string bookFragment)
{
if (id >= books.Count) return NotFound();
books[id] = books[id] + " " + bookFragment; // Simple patch logic
return Ok(books[id]);
}
[HttpDelete("{id}")]
public IActionResult Delete(int id)
{
if (id >= books.Count) return NotFound();
books.RemoveAt(id);
return NoContent();
}
}
[Required], [Range], and custom validators to prevent injection attacks and malformed requests.POST, PUT, PATCH, and DELETE with proper authentication (e.g., JWT) and role/claims-based authorization.POST, PUT, or DELETE routes.POST, PUT, and PATCH operations that carry sensitive payloads.GET or POST endpoints, especially in public APIs.PUT, PATCH, and DELETE operations are idempotent to avoid unintended side effects from repeated requests.400 Bad Request, 401 Unauthorized, 404 Not Found, 500 Internal Server Error) to guide client behavior and debugging.POST, PUT, and DELETE actions, to aid in auditing and troubleshooting.Content-Type (e.g., application/json) and validate it server-side to prevent parsing errors or security issues.GET to perform updates or deletions—this breaks REST principles and can lead to security flaws.curl -X GET https://localhost:5001/api/books[ApiController] to enable automatic model validation and binding.200 OK, 201 Created, 204 No Content, 400 Bad Request, 404 Not Found, etc.[Required], [StringLength], and other data annotations.PUT for full updates and PATCH for partial updates.DELETE operations are protected with proper authorization policies.CreatedAtAction or CreatedAtRoute when returning newly created resources.[HttpGet("{id:int}")]). | Middleware in ASP.NET Core | Filters in ASP.NET Core | |