C# 在asp.net web api中,从对象中的列表中删除项时使用的路径
我在服务器上有一个名为foo的实体,它有一个分配给它的条列表。我希望能够从foo中删除一个条 然而,我不想更新客户端并发送整个foo,因为foo是一个大对象,所以如果我只是从foo中删除一个条,那么每次都要发送很多Json 我只想向下发送该条,然后将其从foo实体中删除 我有我的课C# 在asp.net web api中,从对象中的列表中删除项时使用的路径,c#,asp.net,rest,asp.net-web-api,C#,Asp.net,Rest,Asp.net Web Api,我在服务器上有一个名为foo的实体,它有一个分配给它的条列表。我希望能够从foo中删除一个条 然而,我不想更新客户端并发送整个foo,因为foo是一个大对象,所以如果我只是从foo中删除一个条,那么每次都要发送很多Json 我只想向下发送该条,然后将其从foo实体中删除 我有我的课 public class Foo { public Foo() { Bars = new Collection<Bar>(); } public
public class Foo
{
public Foo()
{
Bars = new Collection<Bar>();
}
public ICollection<Bar> Bars { get; set; }
}
通过javascript(coffeescript)发送请求
我只是不确定使用哪条路线,我试过PUT,但它不喜欢,我可能做错了。我真的不确定在这种情况下我应该使用什么路线
public class BarController : ApiController
{
public void RemoveBarFromFoo(int fooId, Bar bar)
{
// get the foo from the db and then remove the bar from the list and save
}
}
我的问题是:我应该通过什么途径来实现这个目标?或者如果我走错了路,我该怎么办
谢谢,Neil为了遵循标准的RESTful约定,您正在使用的HTTP动词必须是DELETE,操作名称必须是
DELETE
。此外,此操作不应将条形图对象作为参数。只有barId
,因为这就是客户端发送的全部内容:
public class BarController : ApiController
{
public void Delete(int fooId, int barId)
{
// get the foo from the db and then remove the bar from the list and save
}
}
你打电话:
$.ajax({
url: 'api/foo/1/bar/1',
type: 'DELETE',
success: function(result) {
}
});
现在,yuo可以从路由定义中删除该操作,因为它是HTTP动词,指示应该调用哪个操作:
routes.MapHttpRoute(
name: "fooBarRoute",
routeTemplate: "api/foo/{fooId}/bar/{barId}",
defaults: new { controller = "Bar" }
);
$.ajax({
url: 'api/foo/1/bar/1',
type: 'DELETE',
success: function(result) {
}
});
routes.MapHttpRoute(
name: "fooBarRoute",
routeTemplate: "api/foo/{fooId}/bar/{barId}",
defaults: new { controller = "Bar" }
);