错误:404未找到错误:405方法不允许错误:错误请求

时间:2015-04-16 06:07:47

标签: asp.net-mvc

> Showcontroller.js:

app.controller("ShowempController", function ($scope, $location, CRUDservices, SharedData) {
    loadRecords();
    function loadRecords() {
    var PromiseGetEmpdet = CRUDservices.selectEmployees();
    PromiseGetEmpdet.then(function (pl) { $scope.Empdet = pl.data },
        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");
    }
}
});

当我尝试执行我的asp.net mvc项目时,它会在CRUDservices.selectemployees附近的这段代码中显示错误:405方法未找到,当我检查元素时,它还会指出错误:未找到404资源, https://localhost5159/bundles/bootstrap

> Module.js:

var app = angular.module("ApplicationModule", ['ngRoute']);
app.factory("SharedData", function () {
return { value: 0 }
});
app.config(['$routeProvider', '$locationProvider',   function($routeProvider,$locationProvider) {
$routeProvider.when('/Showemp',
    {
        templateUrl: 'Empdet/Showemp',
        controller: 'ShowempController'
    });
$routeProvider.when('/Addemp',
    {
        templateUrl: 'Empdet/Addemp',
        controller: 'AddempController'
    });
$routeProvider.when('/Editemp',
    {
        templateUrl: 'Empdet/Editemp',
        controller: 'EditempController'
    });
$routeProvider.when('/Deleteemp',
    {
        templateUrl: 'Empdet/Deleteemp',
        controller: 'DeleteempController'
    });
$routeProvider.otherwise(
    {
        redirectTo: '/'    
    });
$locationProvider.html5Mode(true).hashPrefix('!')
}]);

> EmpdetController.cs:

using System;
using System.Collections.Generic;
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();


    public IQueryable<Empdet> SelectEmployees()
    {
        return db.Empdets;
    }

    [ResponseType(typeof(Empdet))]
    public IHttpActionResult SelectEmployee(int Id)
    {
        Empdet empdet = db.Empdets.Find(Id);
        if (empdet == null)
        {
            return NotFound();
        }

        return 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);
    }
}

}

&GT; Showemp.cshtml:

@{
    ViewBag.Title = "Show EmpDetails";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>List of Employees</h2>
<a ng-click="Addemp()"> Add Employee </a>
<br />
<table>
<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="EmpDetails 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><a ng-click="Editemp(Empdet.Id)">Edit</a></td>
        <td><a ng-click="Deleteemp(Empdet.Id)">Delete</a></td>
    </tr>
</tbody>
</table>
<div>{{error}}</div>

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;
};
});

EmpdetailsController.cs:

using System;
using System.Collections.Generic;
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();


    public HttpResponseMessage SelectEmployees(Empdet empdet)
    {
        return Request.CreateResponse(HttpStatusCode.OK, empdet);
    }

    [HttpGet]
    public HttpResponseMessage SelectEmployee(int id, Empdet empdet)
    {
        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);
    }
}

}

webAPIconfig.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace Task1
{
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}
}

Routeconfig.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace Task1
{
public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id =  UrlParameter.Optional }
        );
    }
}
}

Homecontroller.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Task1.Controllers
{
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View("About");
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View("Contact");
    }
}
}

Empdetcontoller.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Task1.Controllers
{
public class EmpdetController : Controller
{
    //
    // GET: /Empdet/

    public ActionResult Index()
    {
        return View("Index");
    }
    public ActionResult Addemp()
    {
        return View("Addemp");
    }

    public ActionResult Showemp()
    {
        return View("Showemp");
    }

    public ActionResult Editemp()
    {
        return View("Editemp");
    }

    public ActionResult Deleteemp()
    {
        return View("Deleteemp");
    }
}
}

错误:&#34;参数字典包含参数&#39; id&#39;的空条目。非可空类型的System.Int32&#39; for method&#39; System.Net.Http.HttpResponseMessage SelectEmployee(Int32,Task1.Models.Empdet)&#39;在&#39; Task1.Api.Controllers.EmpdetController&#39;。可选参数必须是引用类型,可以为空的类型,或者声明为可选参数。&#34;

请帮助我解决这个问题,我对这个错误的bcoz感到压抑超过两天。

0 个答案:

没有答案