首页 > 解决方案 > ASP.NET MVC API 操作返回 404,所有其他操作都有效

问题描述

我有一个调用 ASP.NET WEB API 的 Angular 应用程序。我在其中一个 api 控制器上添加了一个方法,它返回 404。可以毫无问题地调用同一控制器上的所有其他方法。该应用程序在 IIS 上本地托管。

API 动作:

[ActionName("GetOrgChart")]
[ResponseType(typeof(List<Object>))]
[HttpPost]
public IHttpActionResult GetOrgChart(string id)
{
    EmployeeMasterPTOCoordinator emPTOCnator = new EmployeeMasterPTOCoordinator();
    List<Object> orgChartData = emPTOCnator.GetOrgChart()
    return Ok<List<Object>>(orgChartData);
}

来自 Angular 应用程序的 HTTP 发布调用:

function GetOrgChart(id) {
   var deferred = $q.defer();
   $http({
        method: 'POST', cache: false,
        url: 'localhost:81/api/Employee/EmployeeMaterPTO/GetOrgChart',
        data: id
    }).success(function(data){
        deffered.resolve(data);
    }).error(function(data){
        deffered.resolve(data);
    })
    return deffered.promise;
}

关于可能导致这种情况的任何想法?我尝试从 IIS 的应用程序池中删除 api 并重新添加它。我已经查看了所有堆栈溢出并且无法找到解决方案

标签: c#asp.netangularjsasp.net-web-apihttp-status-code-404

解决方案


删除 [HttpPost] 并将 [ActionName] 替换为

[Route("~/api/Employee/EmployeeMaterPTO/GetOrgChart/{id?}")]
public IHttpActionResult GetOrgChart(string id)

添加到您的 route.config 的顶部

 routes.MapMvcAttributeRoutes();

并从请求中删除“ data: id ”

method: 'GET',
        url: 'http://localhost:81/api/Employee/EmployeeMaterPTO/GetOrgChart/'+id
     

推荐阅读