【问题标题】:ASP.NET MVC API Action Returning 404, All Other Actions WorkASP.NET MVC API 操作返回 404,所有其他操作都有效
【发布时间】:2021-07-21 04:52:18
【问题描述】:

我有一个调用 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 Post 调用:

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.net angularjs asp.net-web-api http-status-code-404


    【解决方案1】:

    删除 [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
         
    

    【讨论】:

    • 不幸的是,这对我不起作用。我有一种感觉是因为当我更改 c# 代码时 dll 文件没有更新。
    • 是http,因为我在本地开发。完整的网址是:'localhost:81/api/Employee/EmployeeMaterPTO/GetOrgChart'
    • @JacobNarayan 是的,我相信您的任何 API 路由更改都需要重新构建才能发布以供您的客户使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 2013-06-12
    • 2014-11-22
    相关资源
    最近更新 更多