在服务器中部署后找不到Web API

时间:2019-09-06 07:08:17

标签: angularjs api asp.net-web-api http-status-code-404 asp.net-web-api-routing

我已经在测试服务器上部署了Angularjs Web API应用程序(端口为9021,站点名称为Production),当我点击URL时

http://localhost:9021/api/Item/GeAreas?P_Id=US99 

在测试服务器上,我成功获得了Web API的响应。

这是api调用的代码:

 function getAreas(P_Id) { 
        $http({
            method: 'GET',
            url: 'http://ps-test.aars/api/Item/GetAreas', 
        // url: 'http://ps-test.aars/Production/api/Item/GetAreas', // error msg: the server responded with a status of 404 (Not Found)                                             /Production/api/Item/GetAreas?P_Id=US99 
            params: { P_Id: P_Id },
            headers: { 'Content-Type': 'application/json; charset=utf-8', 'dataType': 'json' }
        }).then(function successCallback(response) {
            $scope.Areas = response.data;             
            }, function errorCallback(response) { }             
        });
    }

web.config

 <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
 </handlers>

当我点击URL时:

http://ps-test.aars/Production/Summary.html?P_Id=US99 

在我的本地系统上,出现错误:

  

无法加载资源:服务器响应状态为404(未找到)/ api / Item / GetAreas?P_Id = US99

不确定是什么问题。

3 个答案:

答案 0 :(得分:1)

尝试通过如下所示的api调用代码将端口号附加到您的网址中

 url: 'http://ps-test.aars:9021/api/Item/GetArea'

答案 1 :(得分:0)

这取决于您在生产服务器上设置IIS应用程序的方式以及响应的URL。

转到生产服务器,转到IIS,找到您的应用程序,然后从IIS中打开它,它将在浏览器中打开,并显示设置使用的基本URL。只需在其后添加其余部分即可。当您从实际服务器上运行API时,此方法将起作用。

然后,您需要从计算机上调用它,因此需要确保允许从计算机上对其进行击打。如果不是,则需要打开端口,并确保允许您的计算机访问该生产服务器。

恐怕我不会从本地生产转到直接生产。

答案 2 :(得分:0)

生产服务器上是否还有其他任何web.config文件可能会造成干扰?

我有一个Web API 2项目发布在一个域名的子文件夹中,该项目承载了它自己的ASP.net项目。来自该主应用程序的web.config被继承到Web API 2项目的web.config中,这会干扰路由。可能是因为使用了URL重写模块。为了解决这个问题,我在主项目中stopped web.config inheritance completely

  <location path="." inheritInChildApplications="false"> 
    <system.web> 
    … 
    </system.web> 
  </location>

不确定这是否是您的问题,但这是一回事。