Chciałbym zapewnić REST API w ten sposób:Prawidłowo obsługę zagnieżdżonych zasobów w ASP.net MVC 4 WebAPI routingu
GET /api/devices
POST /api/devices
PUT /api/devices/1
DELETE /api/devices/1
To jest moja konfiguracja:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
A są to Działania:
public IEnumerable<Device> Get()
{
//return all devices
}
public Devices Get(id)
{
//return a specific devices
}
i tak dalej.
Problem pojawia się, gdy chcę, aby obsłużyć zagnieżdżone zasoby:
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
To mój configration do nich:
config.Routes.MapHttpRoute(
name: "NestedApi",
routeTemplate: "api/{controller}/{parentResourceId}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Problem pojawia się, gdy próbuje GET i POST do zasobu zagnieżdżonego :
[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
}
Jest to oczywiście nieskuteczne, ponieważ istnieją dwie akcje z ten sam podpis.
Chciałbym usłyszeć od sposobu realizacji tego z najbardziej relaksującego podejścia
To wygląda na duplikat http://stackoverflow.com/q/10783946/326110 i http://stackoverflow.com/questions/9594671/nested-resources-in-asp-net-mvc-4-webapi/16094056 # 16094056 –