Parameter Binding: กระบวนการตั้งค่า Parameter ให้ Controller Method
[FromUri] Attribute
Bind Parameter จาก URI (Query String) ใช้ ModelBinderAttribute ผ่าน IUriValueProviderFactory
public IEnumerableGet([FromUri] string id, [FromUri] string name) { return new string[] { $"The Id of the Student is {id}", $"The Name of the Student is {name}" }; }
เรียก: https://localhost:58174/api/demo?id=1&name=Mark

[FromBody] Attribute
Bind Parameter จาก Request Body ใช้ Input Formatter อ่าน Body (JSON, XML, Form Data) สำหรับ Complex Type เท่านั้น
// Model
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
// Controller
public IEnumerable Get([FromBody] Student student)
{
return new string[]
{
$"The Id of the Student is {student.Id}",
$"The Name of the Student is {student.Name}"
};
}
ส่ง JSON ใน Body ผ่าน Postman:

สรุปความแตกต่าง
| คุณสมบัติ | [FromUri] | [FromBody] |
|---|---|---|
| แหล่งข้อมูล | Query String / Route Data | Request Body |
| รองรับ Type | Primitive & Complex | Complex Type (แนะนำ) |
| หลาย Parameter | ได้หลายตัว | ได้แค่ 1 ตัวต่อ Request |
| GET Request | เหมาะสม | ไม่แนะนำ (บาง Client ไม่ส่ง Body) |