[英]Not found error while calling apicontroller from angularjs file
我正在使用以下流程构建应用程序我的控制器将调用视图。 在视图中,我正在调用一个 js 文件,该文件具有 angularjs 代码并调用 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
}
看法 :
在这个视图中单击一个按钮,我正在加载另一个视图以及 Javascript 文件,该文件具有名为 EverifyModule.js 的角度代码
<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
}
}
模型绑定器希望您的CompanyId
参数放置在 URI 中,但您将其发送到请求正文中。
明确告诉您的操作方法您正在发送正文中的参数:
public async Task<IHttpActionResult> GetEmployeeList([FromBody] int CompanyId)
看起来 GetEmployeeList 方法接受一个参数,所以也许您只需要向它传递一个参数? 如: http://localhost:52096/api/EVerify/GetEmployeeList/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.