WebApi - การดำเนินการ GET หลายรายการพร้อมลายเซ็นเหมือนกัน - ปัญหาการกำหนดเส้นทาง

ความเป็นมาของคำถาม:

ฉันมีคอนโทรลเลอร์ตัวเดียวภายใน 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 สองวิธีร่วมกันซึ่งมีลายเซ็นเดียวกัน


person user1352057    schedule 29.06.2016    source แหล่งที่มา
comment
ตรวจสอบคำตอบของฉันที่นี่: stackoverflow.com/questions/37608637/   -  person Marcus Höglund    schedule 29.06.2016


คำตอบ (3)


ฉันอาจจะไม่เข้าใจประเด็น แต่ทำไมคุณไม่ปรับเส้นทางแบบนี้ (โปรดสังเกตชื่อการดำเนินการในเทมเพลตเส้นทาง):

config.Routes.MapHttpRoute(
  name: "Messages",
  routeTemplate: "v2/api/People/GetMessages/{id}",
  defaults: new { action = "GetMessages", id = RouteParameter.Optional });

config.Routes.MapHttpRoute(
   name: "Employees",
   routeTemplate: "v2/api/People/GetEmployeeList/{id}",
   defaults: new { action = "GetEmployeeList", id = RouteParameter.Optional});

อย่างไรก็ตาม ฉันอยากจะเลือกใช้ตัวเลือกทั่วไปมากกว่า:

config.Routes.MapHttpRoute(
  name: "People",
  routeTemplate: "v2/api/People/{action}/{id}",
  defaults: new { id = RouteParameter.Optional });

วิธีนี้ทำให้คุณสามารถครอบคลุมการดำเนินการทั้งสองได้ในเส้นทางเดียว มีเหตุผลใดบ้างที่จะไม่เลือกตัวเลือกนี้?

person Anton    schedule 29.06.2016

คุณไม่จำเป็นต้องมีสองเส้นทาง อันเดียวก็พอแล้ว ลองเปลี่ยนเส้นทางของคุณเป็นดังต่อไปนี้

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

คุณสามารถลองระบุเส้นทางของคุณแยกกัน แต่คุณไม่ควรจะต้องระบุ ตรวจสอบให้แน่ใจว่าการเรียกเว็บ API ของคุณถูกต้อง 100%

person fjompen    schedule 29.06.2016

แล้วการระบุเส้นทางของแต่ละวิธีแยกกันล่ะ?

[RoutePrefix("api/people")]
public class MyController : ApiController
{
    [HttpGet]
    [Route("messages")]
    public string GetMessages()
    {
      return "Get Messages Endpoint";
    }

    [HttpGet]
    [Route("employees")]
    public string GetEmployeeList()
    {
        return "Get Employees Endpoint";
    }
}    

ดังนั้นคุณจะมี URL ของคุณ

/v2/api/people/messages
/v2/api/people/employees

IMHO ซึ่งดูสงบกว่าตัวอย่างของคุณเล็กน้อย

person Václav Holuša    schedule 29.06.2016