我有我的角度控制器像Put和Get方法這樣定義。我新來角JS。 get方法按預期工作,但Put方法沒有被調用。我遵循教程之一併實施。儘管在教程中,他們使用REST服務URL而不是控制器方法。我在這裏使用MVC控制器而不是webAPI。角JS使用ASP.NET MVC的Put方法
public JsonResult GetEmployee(int id)
{
Employee empDetail = emp.GetEmployees().FirstOrDefault(i => i.EmpID == id);
return Json(empDetail, JsonRequestBehavior.AllowGet);
}
public JsonResult PutEmployee(int id, Employee updtEmp)
{
updtEmp.EmpID=id;
int index = emp.GetEmployees().FindIndex(i => i.EmpID == updtEmp.EmpID);
emp.GetEmployees().RemoveAt(index);
emp.GetEmployees().Add(updtEmp);
return Json(emp.GetEmployees(), JsonRequestBehavior.AllowGet);
}
這是我的角度廠和控制方法
myservice.factory('empFactory', function ($resource) {
return $resource('../../Employee/GetEmployee/:EmpID', { EmpID: '@EmpID' },
{
show: { method: 'GET' },
update: { method: 'PUT', params: { Employee: '@employee' } }
});
});
myApp.controller('empDetailController', function ($scope, empFactory, $routeParams) {
$scope.Employee = empFactory.show({ EmpID: $routeParams.EmpID });
$scope.UpdateEmp = function() {
// alert($scope.Employee.FirstName);
var employee=$scope.Employee;
empFactory.update({ EmpID: $routeParams.EmpID, Employee: employee })
};
});
你有[在IIS中啓用PUT爲ASP.NET MVC(http://stackoverflow.com/questions/12440277/how-do-i-enable-http -put-和刪除換ASP淨MVC功能於IIS)? – Jasen
嗨Jasen,我已啓用Put ..請參閱以下部分中的我的評論 – TechQuery
由於MVC控制器不會像WebAPI那樣將動作名稱映射到動詞,因此您需要用適當的動詞'[HttpPut]'來修飾控制器。這意味着您需要使用兩個具有不同URL的請求來設置角度服務,而不是試圖超載一個請求。 – Jasen