HTTP Verbs (POST, GET, PUT, PATCH, DELETE) เป็นส่วนสำคัญของ Uniform Interface ตรงกับ CRUD Operations ใน Web API สามารถตั้งชื่อ Action Method ตรงกับ Verb ได้ (Get, Post, Put, Delete) หรือต่อยอดชื่อ เช่น GetAllStudents()
ตัวอย่าง Controller ธรรมดา
public class DemoController : ApiController
{
public IHttpActionResult GetAllStudents() { /* Retrieve data */ return Ok(); }
public IHttpActionResult Post([FromBody]Student student) { /* Insert */ return Ok(); }
public IHttpActionResult Put([FromBody]Student student) { /* Update */ return Ok(); }
public IHttpActionResult Delete(int id) { /* Delete */ return Ok(); }
}
ใช้ Http Verb Attribute เพื่อจำกัดการเข้าถึง
using DemoWebApplication.Models;
using System.Collections.Generic;
using System.Web.Http;
namespace DemoWebApplication.Controllers
{
public class DemoController : ApiController
{
[HttpGet] // HttpVerb Attribute
public IHttpActionResult FetchStudentsList()
{
List students = new List
{
new Student { Id = 1, Name = "Mark" },
new Student { Id = 2, Name = "John" }
};
return Ok(students);
}
}
}
ทดสอบเรียกด้วย GET → สำเร็จ:

ทดสอบเรียกด้วย POST → ได้ 405 Method Not Allowed:

Attribute อื่นๆ: [HttpPost], [HttpPut], [HttpPatch], [HttpDelete], [HttpOptions], [HttpHead]