2015-04-17 218 views
0

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引用”。但它仍然没有触及文件并且没有执行。请帮忙!!!

+0

return $ http.get(“/ api/Empdet/SelectEmployees”);改变返回$ http.get(“/ api/SelectEmployees”),并检查它是否工作 – Reena

+0

我看到你的控制器'SelectEmployees'被参数化,但我看不到任何传递的Ajax调用?检查一下 。 –

+3

缩小你的问题,只添加relvent代码,请不要转储你的整个代码。 – Satpal

回答

3

解决方案1 ​​

您需要分配[ActionName("Name")]属性为您行动

因为网络API只服用得到,POST,PUT,为删除方法名GET并发帖原因

if you change the action name, then you need set the ActionName attribute

[ActionName("SelectEmployees")] 
[HttpGet] 
    public HttpResponseMessage SelectEmployees(Empdet empdet) 
    { 
     Collection<Empdet> Empdets =new Collection<Empdet>(db.Empdets.ToList()); 
     return Request.CreateResponse(HttpStatusCode.OK, Empdets); 
    } 

解决方案2

  • 还请检查Parameters。您应该将正确的参数对象和值传递给您的控制器操作。

解决方案3

验证您的WebAPI的配置文件

你的路让您的路线的URL看起来像

config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{action}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 
  • 以及您的网址应该看像$http.get("/api/Empdet/SelectEmployees")

我希望你能从我的钥匙答案:)

+0

感谢您的帮助和问题现在解决,但我执行它从数据库中加载记录,并将其存储到范围,但它没有得到显示 – Frankline

+0

请通过新问题 –

+0

问我新问题绑定数据正确,但它没有显示它来检查使用console.log中的路线,但它携带的数据作为对象数组,但不显示帮助。 – Frankline

1

解决它,如果你不想给任何动作名称上方
的建议还可以使用回$ HTTP。 get(“/ api/Empdet”)