ความเป็นมาของคำถาม:
ฉันมีคอนโทรลเลอร์ตัวเดียวภายใน WebApi ของฉันซึ่งมีวิธีดำเนินการสองวิธี วิธีดำเนินการทั้งสองนี้เป็นทั้ง GET และไม่มีพารามิเตอร์
วิธีดำเนินการวิธีหนึ่งชื่อ 'GetMessages' และอีกวิธีหนึ่งคือ 'GetEmployeeList'
ปัญหา:
นี่คือวิธีดำเนินการต่อไปนี้ในคอนโทรลเลอร์ของฉัน:
[HttpGet]
public string GetMessages()
{
return "Get Messages Endpoint";
}
[HttpGet]
public string GetEmployeeList()
{
return "Get Employees Endpoint";
}
นี่คือเส้นทางศุลกากรที่ฉันตั้งไว้ด้วย WepApiConfig
config.Routes.MapHttpRoute(
name: "Messages",
routeTemplate: "v2/api/People/{action}/{id}",
defaults: new { action = "GetMessages", id = RouteParameter.Optional });
config.Routes.MapHttpRoute(
name: "Employees",
routeTemplate: "v2/api/People/{action}/{id}",
defaults: new { action = "GetEmployeeList", id = RouteParameter.Optional});
เมื่อฉันเรียก pf URL ต่อไปนี้:
/v2/api/People/GetMessages
/v2/api/People/GetEmployeeList
ฉันได้รับข้อความยกเว้นต่อไปนี้ที่ระบุว่าการดำเนินการหลายอย่างตรงกับคำขอ URL:
Multiple actions were found that match the request: System.Net.Http.HttpResponseMessage GetMessages() on type Persons.PeopleController System.Net.Http.HttpResponseMessage GetEmployeeList() on Persons.PeopleController
ฉันจะแก้ไขสิ่งนี้ได้อย่างไร ฉันจึงใช้วิธีการดำเนินการ GET สองวิธีร่วมกันซึ่งมีลายเซ็นเดียวกัน