C# 错误:找不到404
Services.js:C# 错误:找不到404,c#,angularjs,asp.net-mvc,C#,Angularjs,Asp.net Mvc,Services.js: 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);
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;
};
});
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);
}
}
}
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");
};
});
<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:
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;
};
});
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);
}
}
}
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");
};
});
<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>
Showemp.cshtml:
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;
};
});
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);
}
}
}
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");
};
});
<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>
雇员名单
添加员工
身份证件
照片文件
名字
姓氏
电子邮件
年龄
图文
{{Empdet.Id}
{{Empdet.PhotoFile}
{{Empdet.FirstName}
{{Empdet.LastName}
{{Empdet.Email}
{{Empdet.Age}
{{Empdet.PhotoText}}
{{error}}
每当我尝试执行此程序时,它都会显示错误:404未找到,它没有在Showempcontroller.js中点击SelectEmployees,我在Empdetcontroller.cs文件中明确提到,我使用“SelectEmployees”选择所有员工,对于单个数据检索,我使用“SelectEmployeed并通过Id引用它”。但它仍然没有命中文件,也没有执行。请帮忙 解决方案1 您需要为您的操作分配
[ActionName(“Name”)]
属性
因为Web Api只接受get、post、put、delete方法名,原因是get和post
如果更改操作名称,则需要设置ActionName属性
像
- 您的url应该类似于http.get(“/api/Empdet/SelectEmployees”)
我希望你能从我的关键答案中找到答案:)解决方案1 您需要为您的操作分配
[ActionName(“Name”)]
属性
因为Web Api只接受get、post、put、delete方法名,原因是get和post
如果更改操作名称,则需要设置ActionName属性
像
- 您的url应该类似于http.get(“/api/Empdet/SelectEmployees”)
我希望您能从我的关键答案中解决这个问题:)如果您不想给出上面建议的任何操作名称
如果您不想按照上面的建议给出任何操作名称,也可以使用return$http.get(“/api/Empdet”)
您还可以使用return$http.get(“/api/Empdet”)return$http.get(“/api/Empdet/SelectEmployees”);更改为返回$http.get(“/api/SelectEmployees”)并检查它是否工作。我看到您的控制器
SelectEmployees
已参数化,但我没有看到从ajax调用传递任何消息?检查它。缩小您的问题范围,只添加相关代码,请不要转储整个代码。您还可以使用return$http.get(“/api/Empdet”),您应该检查http网络流量,以查看调用的URL到底是什么。在您喜爱的浏览器中打开“开发者”工具,或使用外部软件包,如Fiddler.return$http.get(“/api/Empdet/SelectEmployees”);更改为返回$http.get(“/api/SelectEmployees”)并检查它是否工作。我看到您的控制器SelectEmployees
已参数化,但我没有看到从ajax调用传递任何消息?检查它。缩小您的问题范围,只添加相关代码,请不要转储整个代码。您还可以使用return$http.get(“/api/Empdet”),您应该检查http网络流量,以查看调用的URL到底是什么。在您喜爱的浏览器中打开“开发者”工具,或使用Fiddler等外部软件包。感谢您的帮助,问题现在已解决,但当我执行时,从数据库加载记录并将其存储到范围,但未显示。请通过新问题询问新问题。我正在正确绑定数据,但没有显示它用于检查正在使用的路由console.log,但它以对象数组的形式携带数据,但不显示pls help。请确定我将提出一个新问题。向我展示您的数组对象:(感谢您的帮助,现在问题已经解决了,但当我执行时,从数据库加载记录并将其存储到范围中,但没有显示出来。请通过新问题询问新问题。我正确绑定了数据,但没有显示它以检查正在使用的路由console.log,但它以对象数组的形式携带数据但不显示请帮助。确定我将提出一个新问题。向我显示您的数组对象:(