【发布时间】:2015-10-23 14:21:19
【问题描述】:
我已使用此 [模板](https://visualstudiogallery.msdn.microsoft.com/48d928e3-9b5c-4faf-b46f-d6baa7d9886c"当您将鼠标悬停时显示此文本") 制作了这个项目。
这是一个简单的任务管理器,您可以在其中创建、完成和删除任务。
我通过几乎完全复制模板作者的代码来启动并运行它并添加了一个额外的按钮。
我下面的代码是他的方法和我的方法的例子。
代码
他完成任务的方式:
前端:
// called by ng-click="complete($index)"
$scope.complete = function(index)
{
$http.post('/api/WS_Todo/CompleteTodoItem/' + $scope.todoList[index].id)
.success(function (data, status, headers, config) {
$scope.getList();
});
}
后端:
[HttpPost]
[Authorize]
async public Task<HttpResponseMessage> CompleteTodoItem(int id)
{
var item = db.todos.Where(t => t.id == id).FirstOrDefault();
if (item != null)
{
item.completed = true;
await db.SaveChangesAsync();
}
return Request.CreateResponse(HttpStatusCode.Accepted);
}
我重新打开任务的方式:
前端:
// called by ng-click="reopen($index)"
$scope.reopen = function(index)
{
$http.post('/api/WS_Todo/ReopenTodoItem', +$scope.todoList[index].id)
.success(function (data, status, headers, config) {
$scope.getList();
});
}
后端:
[HttpPost]
[Authorize]
async public Task<HttpResponseMessage> ReopenTodoItem(int id)
{
var item = db.todos.Where(t => t.id == id).FirstOrDefault();
if (item != null)
{
item.completed = false;
await db.SaveChangesAsync();
}
return Request.CreateResponse(HttpStatusCode.Accepted);
}
问题
他的代码按预期工作。
我的代码给了我:
POST http://localhost:33651/api/WS_Todo/ReopenTodoItem 404(未找到)angularjs:9734
当两种方法都在同一个前端和后端控制器中时,我看不出路由应该是一个问题。
【问题讨论】:
标签: c# asp.net angularjs asp.net-web-api