[英]ASP.Net WebAPI with Angularjs 404 not found
我已經使用此[Template]( 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
而且,當兩種方法都在同一個前端和后端控制器中時,我看不到路由應該是什么問題。
如果我是我,我會在我的webApi方法上指定一個route屬性:
[Route("/api/WS_Todo/CompleteTodoItem/{id}")]
然后使用正確的語法從最前面調用該網址。 其他方法相同。
經過數次嘗試之后,使用了不同的方法..我在網址>。<中被逗號打敗了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.