C# $http.delete服务器请求不工作
我正在尝试对API执行http delete请求,但调用失败。Get和Post方法可以很好地工作,但delete调用不能。我的猜测是,问题出在客户端代码上,但不知道是什么。这是客户的代码C# $http.delete服务器请求不工作,c#,asp.net,angularjs,http,asp.net-web-api,C#,Asp.net,Angularjs,Http,Asp.net Web Api,我正在尝试对API执行http delete请求,但调用失败。Get和Post方法可以很好地工作,但delete调用不能。我的猜测是,问题出在客户端代码上,但不知道是什么。这是客户的代码 $http.delete("http://localhost:69174/api/posts", postId) .then(function (response) { // sucess delete v
$http.delete("http://localhost:69174/api/posts", postId)
.then(function (response) {
// sucess delete
viewModel.posts.delete(response.data);
viewModel.newPost = {};
}, function () {
// failed to delete
viewModel.errorMessage = "Failed to delete post";
})
.finally(function () {
});
这是API函数
// DELETE api/values/5
[HttpDelete("{id}")]
public async Task Delete(Guid id)
{
await _postsRepo.DeletePostAsync(id);
}
还有一个问题,从客户端对API进行http调用,直接从my AngularJs控制器或ASP.NET控制器发出请求的最佳做法是什么?这允许调用成功,但不确定即使在删除时也通过url传递id是否是一种好的做法
$http.delete("http://localhost:69174/api/posts/" + postId)
.then(function (response) {
...
}, function () {
...
})
.finally(function () {
...
});
您确认开发人员工具中的目标URL了吗?是的,我确认了。我能够开始工作并发布了我是如何做到的,但我想知道为什么我以前的实现无法使用它,以及是否有更好的解决方案“不确定在删除url时通过url传递id是否是一种好做法”,这很好,并且是RESTful设计的标准。如果您的API的其余部分遵循REST设计的基础,请考虑使用<代码> $Reals,而不是<代码> $HTP以获得更简单的抽象。是的,其余的API遵循REST设计。我该怎么做?我试着在我的代码中用$resource替换$http,但它没有打电话给APIoh,感谢您对良好实践问题的反馈:)消费
$resource
与$http
的工作方式有点不同。有很多关于这个主题的指南和文档。作为一个起点。