未找到错误404

时间:2015-04-17 09:24:46

标签: 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);
};

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>

每当我尝试执行此程序时,它都会显示错误:404未找到,它没有在Showempcontroller.js中点击SelectEmployees我在Empdetcontroller.cs文件中提到了我选择的所有员工&#34; SelectEmployees&#34 ;对于我使用的单个数据检索&#34; SelectEmployee并通过Id&#34;引用它。但它仍然没有击中文件而没有执行。请帮助!!!

2 个答案:

答案 0 :(得分:3)

解决方案1 ​​

您需要为操作

分配[ActionName("Name")]属性

因为 Web Api 只为获取和发布原因获取,发布,放置,删除方法名称

  

如果您更改操作名称,则需要设置 ActionName属性

喜欢

[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配置文件中的路径

使您的路线网址看起来像

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

我希望你能从我的关键答案中解决它:)

答案 1 :(得分:1)

如果您不想按照上面的建议提供任何动作名称 您还可以使用return $ http.get(“/ api / Empdet”)