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

时间:2016-02-25 00:44:39

标签: angularjs iis asp.net-web-api n-tier-architecture

我是Web Api的新手,并且使用的是n层架构。我有一个简单的应用程序与AngularJs作为JavaScript框架和使用Web Api作为RESTFul服务。我还有数据访问层业务逻辑层模型的单独图层.Html页面包含在包含Api的同一项目中,在Pages中夹。 这是我的项目结构: enter image description here

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

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

但它正在抛出enter image description here

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

1 个答案:

答案 0 :(得分:1)

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

在我的饱和状态下,我使用ASP.Net MVC _layout.cshtml ,因此我使用@Url.Content("~")获取网站的网址,并将其附加到每个Angular&的前面#39;请求的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, {});
}