自我主题的web api路由到angularJS index.html

时间:2016-01-13 21:31:28

标签: angularjs asp.net-web-api self-hosting

我使用OWIN来自我主机(我没有使用IIS)我的web api,我正在阅读很多关于基于asp.net web api和angularJS的web应用程序。在大多数示例中,api的默认路由如

HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(_baseAddress);
config.Routes.MapHttpRoute(
  name: "DefaultApi",
  routeTemplate: "api/{controller}/{id}",
  defaults: new { id = RouteParameter.Optional }
);

但我不明白到angularJS前端的index.html文件的路由发生了什么?我知道在调用像api/person/5这样的api请求时如何获取数据。但是代码的哪一部分负责将任何未指定的api请求路由到angularJS的主页?

1 个答案:

答案 0 :(得分:0)

使用WebAPI和Index.html的默认路由不是一个好主意。因为调用是yourapp.com/api/xxxx

相反,因为您正在使用IIS(我假设您不使用OWIN进行自托管)。在IIS中,您可以并且应该设置默认页面。

这样,无论是yourapp.comindex.html还是jack.html

,您的网址都会看起来test.html

回答你的问题但是代码的哪一部分负责将任何未指定的api请求路由到angularJS的主页?

app.config(function ($routeProvider) {

    $routeProvider.otherwise({ redirectTo: "/Main" });
});

使用$routeprovider.otherwise进行redirct。如果您将不可识别的路线发送到角度,则在此情况下,否则配置值会将您重定向到main

MSDN页面将引导您在iis中设置默认页面

相关问题