ASP.NET Core Interview Question #122
How do HTTP verb attributes work in ASP.NET Core controllers?
Routing, Controllers & Model Binding Mid-Level Intermediate
Quick Interview Answer
Attributes such as HttpGet, HttpPost, HttpPut, HttpPatch, and HttpDelete restrict an action to specific HTTP methods and can also define route templates.
Detailed Explanation
Attributes such as HttpGet, HttpPost, HttpPut, HttpPatch, and HttpDelete restrict an action to specific HTTP methods and can also define route templates.
Using the correct HTTP verb makes endpoint intent clear. GET is normally used for safe retrieval, POST for creating or submitting operations, PUT for replacement-style updates, PATCH for partial updates, and DELETE for deletion.
Application semantics and idempotency should guide the design rather than choosing verbs only by convention.
Code Example
[HttpGet("{id:int}")]
public IActionResult Get(int id) => Ok();
[HttpPost]
public IActionResult Create(CreateProductRequest request) => Ok();
[HttpDelete("{id:int}")]
public IActionResult Delete(int id) => NoContent();