我正在开发基于MVC4 RC并使用数据库优先实体框架作为我的模型的Web API。
我拥有的2个实体是Item
和Group
。
这两个实体之间存在多对多的关系。
现在,在使用标准HTTP方法(GET
,POST
,PUT
和DELETE
)轻松实现两者的CRUD操作API之后,我来了到了我想要实现组与组之间的绑定和解除绑定的程度。
我尝试了其他动词,例如LOCK
和UNLOCK
,但没有成功(他们似乎不支持它们),并试图以某种方式操纵POST
和{{再次,1}}命令没有成功。
你们中的任何一位好人都知道如何实现这个目标吗?
非常感谢!
答案 0 :(得分:1)
您可以将多对多表示为根资源上的子集合。例如。您有/ items / 1234和/ groups / 4567 - 您可以将组作为子集合作为/ items / 1234 / groups或/ groups / 4567 / items
无论哪种方式同样有效。我通常会使用PUT
来设置关系,然后使用DELETE
删除它 - 有人会说这不是REST,但它在我使用过的场景中运行良好。< / p>
PUT /items/1234/groups/4567
- 在项目1234和第4567组之间创建关系
DELETE /items/1234/groups/4567
- 删除项目1234和第4567组之间的关系
这篇文章给了我很多帮助。当我最后调查这个......
How to handle many-to-many relationships in a RESTful API?
更新:路由
因此,对于这些更复杂的场景,我们最终只使用了更具体的路线。它很快就会变得丑陋,试图将所有东西都塞进一条通用路线中。我们有一套单元测试,可以确保相关的URL被路由到正确的控制器和操作。
// routes
routes.MapHttpRoute(
name: "items.groups",
routeTemplate: "items/{itemId}/groups/{groupId}",
defaults: new { controller = "ItemGroup", groupId = RouteParameter.Optional });
然后,ItemGroupController具有Get,Delete和Put方法。我们这样单元测试...
// unit tests
[Test]
public void PutItemGroup()
{
RoutingResult routingResult = this.GenerateRoutingResult(HttpMethod.Put, "~/items/1234/groups/4567");
Assert.IsNotNull(routingResult);
Assert.AreEqual("ItemGroup", routingResult.Controller);
Assert.AreEqual("Put", routingResult.Action);
Assert.AreEqual("1234", routingResult.RouteData.Values["itemId"]);
Assert.AreEqual("4567", routingResult.RouteData.Values["groupId"]);
}
[Test]
public void GetItemGroups()
{
RoutingResult routingResult = this.GenerateRoutingResult(HttpMethod.Get, "~/items/1234/groups");
Assert.IsNotNull(routingResult);
Assert.AreEqual("ItemGroup", routingResult.Controller);
Assert.AreEqual("GetAll", routingResult.Action);
Assert.AreEqual("1234", routingResult.RouteData.Values["itemId"]);
}
[Test]
public void GetItemGroup()
{
RoutingResult routingResult = this.GenerateRoutingResult(HttpMethod.Get, "~/items/1234/groups/4567");
Assert.IsNotNull(routingResult);
Assert.AreEqual("ItemGroup", routingResult.Controller);
Assert.AreEqual("Get", routingResult.Action);
Assert.AreEqual("1234", routingResult.RouteData.Values["itemId"]);
Assert.AreEqual("4567", routingResult.RouteData.Values["groupId"]);
}
[Test]
public void DeleteItemGroup()
{
RoutingResult routingResult = this.GenerateRoutingResult(HttpMethod.Delete, "~/items/1234/groups/4567");
Assert.IsNotNull(routingResult);
Assert.AreEqual("ItemGroup", routingResult.Controller);
Assert.AreEqual("Delete", routingResult.Action);
Assert.AreEqual("1234", routingResult.RouteData.Values["itemId"]);
Assert.AreEqual("4567", routingResult.RouteData.Values["groupId"]);
}
private RoutingResult GenerateRoutingResult(HttpMethod method, string relativeUrl)
{
HttpConfiguration httpConfiguration = new HttpConfiguration(this.HttpRoutes);
HttpRequestMessage request = new HttpRequestMessage(method, string.Format("http://test.local/{0}", relativeUrl.Replace("~/", string.Empty)));
IHttpRouteData routeData = this.HttpRoutes.GetRouteData(request);
Assert.IsNotNull(routeData, "Could not locate route for {0}", relativeUrl);
this.RemoveOptionalRoutingParameters(routeData.Values);
request.Properties.Add(HttpPropertyKeys.HttpRouteDataKey, routeData);
request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, httpConfiguration);
IHttpControllerSelector controllerSelector = new DefaultHttpControllerSelector(httpConfiguration);
HttpControllerContext controllerContext = new HttpControllerContext(httpConfiguration, routeData, request)
{
ControllerDescriptor = controllerSelector.SelectController(request)
};
HttpActionDescriptor actionDescriptor = controllerContext.ControllerDescriptor.HttpActionSelector.SelectAction(controllerContext);
if (actionDescriptor == null)
{
return null;
}
return new RoutingResult
{
Action = actionDescriptor.ActionName,
Controller = actionDescriptor.ControllerDescriptor.ControllerName,
RouteData = routeData
};
}
private void RemoveOptionalRoutingParameters(IDictionary<string, object> routeValueDictionary)
{
int count = routeValueDictionary.Count;
int index1 = 0;
string[] strArray = new string[count];
foreach (KeyValuePair<string, object> keyValuePair in routeValueDictionary)
{
if (keyValuePair.Value == RouteParameter.Optional)
{
strArray[index1] = keyValuePair.Key;
++index1;
}
}
for (int index2 = 0; index2 < index1; ++index2)
{
string key = strArray[index2];
routeValueDictionary.Remove(key);
}
}
private class RoutingResult
{
public string Controller { get; set; }
public string Action { get; set; }
public IHttpRouteData RouteData { get; set; }
}
干杯, 迪安