मार्ग में नेस्टेड संसाधनों से निपटने मैं इस तरह से बाकी एपीआई प्रदान करने के लिए करना चाहते हैं:ठीक ASP.net MVC 4 WebAPI
GET /api/devices
POST /api/devices
PUT /api/devices/1
DELETE /api/devices/1
यह मेरा विन्यास है:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
और इन कर रहे हैं क्रियाएं:
public IEnumerable<Device> Get()
{
//return all devices
}
public Devices Get(id)
{
//return a specific devices
}
और इसी तरह।
मुद्दा प्रकट होता है जब मैं नेस्ट संसाधनों हैंडल करना चाहते हैं:
GET /api/devices/1/readings
POST /api/devices/1/readings
GET /api/devices/1/readings/1
PUT /api/devices/1/readings/1
DELETE /api/devices/1/readings/1
यह इन के लिए मेरे configration है:
config.Routes.MapHttpRoute(
name: "NestedApi",
routeTemplate: "api/{controller}/{parentResourceId}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
मुद्दे को पता चलता है जब नेस्टेड संसाधन के लिए मिलता है और पोस्ट करने की कोशिश :
[HttpGet]
public String Readings(int parentResourceId)
{
//return a list of readings for the device
}
[HttpPost]
public String Readings(int parentResourceId)
{
//create and return the id of a reading for the device
}
यह निश्चित रूप से विफल रहा है क्योंकि इसमें दो कार्य हैं एक ही हस्ताक्षर।
मैं सबसे RESTful दृष्टिकोण
यह http://stackoverflow.com/q/10783946/326110 और http://stackoverflow.com/questions/9594671/nested-resources-in-asp-net-mvc-4-webapi का डुप्लिकेट जैसा दिखता है/16094056 # 16094056 –