2015-04-17 216 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」)