Services.js:錯誤:404未找到
app.service("CRUDservices", function ($http) {
this.selectEmployees = function() {
return $http.get("/api/Empdet/SelectEmployees");
};
this.selectEmployee = function (id) {
return $http.get("/api/Empdet/SelectEmployee/" + id);
};
this.addEmployee = function (Empdet) {
var request = $http(
{
method: "post",
url: "/api/Empdet/AddEmployee",
data: Empdet
});
return request;
};
this.updateEmployee = function (id, Empdet) {
var request = $http(
{
method: "put",
url: "/api/Empdet/UpdateEmployee/" + id,
data: Empdet
});
return request;
};
this.deleteEmployee = function (id) {
var request = $http(
{
method: "delete",
url: "/api/Empdet/DeleteEmployee/" + id,
data: Empdet
});
return request;
};
});
EmpdetController.cs:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Http.Description;
using Task1.Models;
namespace Task1.Api.Controllers
{
public class EmpdetController : ApiController
{
private EmployeeEntities db = new EmployeeEntities();
[HttpGet]
public HttpResponseMessage SelectEmployees(Empdet empdet)
{
Collection<Empdet> Empdets =new Collection<Empdet>(db.Empdets.ToList());
return Request.CreateResponse(HttpStatusCode.OK, Empdets);
}
[HttpGet]
public HttpResponseMessage SelectEmployee(int? id)
{
var empdet = db.Empdets.Find(id);
if (empdet == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK, empdet);
}
[HttpPut]
public HttpResponseMessage UpdateEmployee(int id, Empdet empdet)
{
if (ModelState.IsValid && id == empdet.Id)
{
db.Entry(empdet).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
[HttpPost]
public HttpResponseMessage AddEmployee(Empdet empdet)
{
if (ModelState.IsValid)
{
db.Empdets.Add(empdet);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, empdet);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = empdet.Id }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
[HttpDelete]
public HttpResponseMessage DeleteEmployee(int id)
{
Empdet empdet = db.Empdets.Find(id);
if (empdet == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
db.Empdets.Remove(empdet);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK, empdet);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
ShowempController.js:
app.controller("ShowempController", function ($scope, $location, CRUDservices, SharedData) {
$scope.loadRecords = function() {
//CRUDservices.selectEmployees().success(function (response) {
// $scope.Employees = response;
//});
console.log('init');
var promiseGetEmpdet = CRUDservices.selectEmployees();
promiseGetEmpdet.then(function (pl) {
console.log(pl);
$scope.Employees = pl.data
console.log($scope.Employees);
},
function (errorpl) {
$scope.error = 'failure loading employee', errorpl;
});
};
$scope.Addemp = function() {
$location.path("/Addemp");
};
$scope.Editemp = function (Id) {
ShareData.value = Id;
$location.path("/Editemp");
};
$scope.Deleteemp = function (Id) {
ShareData.value = Id;
$location.path("/Deleteemp");
};
});
Showemp.cshtml:
<html ng-app="ApplicationModule">
<body>
<div ng-controller="ShowempController" data-ng-init="loadRecords()">
<h2>List of Employees</h2>
<a ng-click="Addemp()">Add Employee </a>
<br />
<table border="1" class="mytable">
<thead>
<tr>
<th>Id</th>
<th>PhotoFile</th>
<th>FirstName</th>
<th>LastName</th>
<th>Email</th>
<th>Age</th>
<th>PhotoText</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="Empdet in Employees">
<td>{{Empdet.Id}}</td>
<td>{{Empdet.PhotoFile}}</td>
<td>{{Empdet.FirstName}}</td>
<td>{{Empdet.LastName}}</td>
<td>{{Empdet.Email}}</td>
<td>{{Empdet.Age}}</td>
<td>{{Empdet.PhotoText}}</td>
<td><input type="button" value="Edit" ng- click="Editemp(Empdet.Id)" /></td>
<td><input type="button" value="Delete" ng-click="Deleteemp(Empdet.Id)" /></td>
</tr>
</tbody>
</table>
<div>{{error}}</div>
</div>
</body>
</html>
每當我嘗試它說錯誤執行該程序:在Empdetcontroller.cs 404沒有找到,它在Showempcontroller.js不打SelectEmployees我已經提到中邦文件選擇所有我使用「SelectEmployees」的員工以及單個數據檢索,我使用了「SelectEmployee並通過Id引用」。但它仍然沒有觸及文件並且沒有執行。請幫忙!!!
return $ http.get(「/ api/Empdet/SelectEmployees」);改變返回$ http.get(「/ api/SelectEmployees」),並檢查它是否工作 – Reena
我看到你的控制器'SelectEmployees'被參數化,但我看不到任何傳遞的Ajax調用?檢查一下 。 –
縮小你的問題,只添加relvent代碼,請不要轉儲你的整個代碼。 – Satpal