如何在 IIS 上的虚拟目录中使用 AngularJs 托管 Web API

How to host Web API with AngularJs inside Virtual Directory on IIS

我是 Web Api 的新手,使用的是 n 层架构。我有一个使用 AngularJs 作为 javascript 框架并使用 Web Api 作为 RESTFul 服务的简单应用程序。我还有数据访问层业务逻辑层模型.[=32的单独层=] 页面包含在包含 Api 的同一项目中,位于 Pages 文件夹中。 这是我的项目结构:

我正在尝试在本地 IIS 上托管该项目。我已将服务器选项从 FirstApp.Web 的属性更改为本地 IIS。 Html 页面显示正常。该页面包含简单的登录表单。使用 Angularjs 服务,我正在调用 Controllers 文件夹下的 Web Api。

this.authenticate = function (obj) {
        return $http.post('/api/Employee/Validate/', obj, {});
    }

但它正在抛出

帮我弄清楚如何在本地 IIS 上托管整个应用程序。

如果您不在根级别托管应用程序,就会发生这种情况 - http://localhost/Pages/Index.html

在我的饱和状态下,我使用 ASP.Net MVC _layout.cshtml,所以我使用 @Url.Content("~") 获取站点的 URL,并将其附加到每个 Angular 请求的 URL.

的前面
// This script tags is placed inside _layout.cshtml
<script type="text/javascript">        
    window.MyApp = {
        rootPath: '@Url.Content("~")'
    };
</script>

用法

this.authenticate = function (obj) {
   return $http.post(window.MyApp.rootPath + 'api/Employee/Validate/', obj, {});
}