从angularjs文件调用apicontroller时未找到错误

时间:2016-04-29 22:44:17

标签: javascript c# angularjs asp.net-mvc asp.net-web-api

我正在使用以下流程构建应用程序   我的控制器将调用视图。在视图内部,我调用一个带有angularjs代码的js文件,调用APIController。

我收到以下错误:

Request URL:http://localhost:52096/api/EVerify/GetEmployeeList
Request Method:POST
Status Code:404 Not Found

我错过了什么或做错了什么。

控制器:

using System.Web.Mvc;

namespace MVC.EVerify.Controllers
{
    [Authorize]
    public class EVerifyController : Controller
    {
        #region Methods
        #region ListEVerify
        public ActionResult ListEVerify()
        {
            if (Request.IsAjaxRequest()) return PartialView();
            return View();
        }
        #endregion
}

查看:

在这个单击按钮的视图中,我正在加载另一个视图以及具有名为EverifyModule.js的角度代码的Javascript文件

<table>
 <tbody>
    <tr ng-repeat="emp in EmployeeInfo">
      <td>{{emp.name}}</td>
       <td>{{emp.hireDate}}</td>
       <td><a class="btn-sm btn-primary pull-right" href="javascript:void(0)"  onclick="LoadViewSelected('/EVerify/EVerify/EVerifySubmit', 'EVerifyModule', 'E-VerifySubmit');">E-Verify</a></td>
     </tr>
  </tbody>
</table>

EVerifyModule.js

var EVerifyModule = angular.module('EVerifyModule', ['angularFileUpload', 'ui.bootstrap', 'angularUtils.directives.dirPagination']);


    EVerifyModule.factory('EVerifyModuleService', ['$http', '$window', function ($http, $window) {

        return {

            GetEmployeeList: function (companyId) {
                return $http({
                    url: '/api/EVerify/GetEmployeeList',
                    method: 'POST',
                    data: companyId
                });
            }
        };
    }]);


    EVerifyModule.controller('EVerifyController', ['$scope', '$http', '$compile', 'EVerifyModuleService', '$modal', '$timeout', function ($scope, $http, $compile, EVerifyModuleService, $modal, $timeout) {

        EVerifyModuleService.GetEmployeeList(58).then(function (response) {
            $scope.EmployeeInfo = response.data.Employees;
        });

EVerifyAPIController:

namespace MVC.EVerify.Controllers
{
    [RoutePrefix("api/EVerify")]
    public class EVerifyAPIController : ApiController
    {
        #region GetEmployeeList

        [HttpPost]
        [Route("GetEmployeeList")]
        public async Task<IHttpActionResult> GetEmployeeList(int CompanyId)
        {
            List<EmployeeBO> employees = new List<EmployeeBO>();

            try
            {
                employees = await EmployeeBL.GetEmployeeList(CompanyId);
            }
            catch
            {
                employees = new List<EmployeeBO>();
            }

            return Ok(new { Employees = employees });
        }

        #endregion

    }
}

2 个答案:

答案 0 :(得分:2)

Model Binder希望您的CompanyId参数放在URI中,但是您将其发送到请求正文中。

明确告诉您的操作方法您正在正文中发送参数:

public async Task<IHttpActionResult> GetEmployeeList([FromBody] int CompanyId)

答案 1 :(得分:0)

看起来GetEmployeeList方法接受一个参数所以你可能只需要传递一个参数?喜欢: http://localhost:52096/api/EVerify/GetEmployeeList/1