未找到错误404 [英] Error: 404 NOT FOUND

查看:189
本文介绍了未找到错误404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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没有找到,它不打SelectEmployees在Showempcontroller.js我有克利在Empdetcontroller.cs上述文件选择所有员工,我使用SelectEmployees,并为一个单一的数据检索我用SelectEmployee和ID参照它。但它仍然不打文件并没有执行。请帮助!

Whenever i try to execute this program it says error: 404 not found, its not hitting SelectEmployees in the Showempcontroller.js i have cleary mentioned in the Empdetcontroller.cs file for selecting all employees i use "SelectEmployees" and for a single data retrieve i used "SelectEmployee and referring it by Id". But still its not hitting the file and not executing. Please Help!!!

推荐答案

解决方案1 ​​

您需要分配 [ActionName(姓名)] 为您的属性的动作

You need to assign the [ActionName("Name")] attribute for your action

由于的网页API 的唯一采取的 GET,POST,放,删除的为方法名的 get和post 的原因

Because Web Api only taking get, post, put,delete method name for the get and post reason

如果您更改的动作的名称,那么你需要设置的 ActionName属性

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


  • 并且还请查看参数。你应该正确的参数对象和值传递到控制器的动作。

  • And also please check the Parameters. You should be pass the correct parameter object and values to your controller actions .

解决方案3

验证您的WebAPI的配置文件你的路。

verify your paths in your WebApi config file

让您的路线网址看起来像

Making your route url look like

 config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );


  • 以及您的网址应该是像 $ http.get(/ API / Empdet / SelectEmployees)

    • And your url should be look like $http.get("/api/Empdet/SelectEmployees")
    • 我希望你可以从我的键接听:)

      I hope you can solve it from my key answer :)

      这篇关于未找到错误404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆