【问题标题】:Angular app on IIS 8.x virtual directory has strange URL behaviorIIS 8.x 虚拟目录上的 Angular 应用程序具有奇怪的 URL 行为
【发布时间】:2014-06-02 16:11:03
【问题描述】:

我在使用 MVC 的 IIS 8.x 上遇到 Angular JS 的问题。我的带有虚拟目录的应用程序索引页面是这样的:

https://myapp.xyz/VirtualDirectory/

该页面加载 Angular 应用程序。我的角度路线设置为:

$routeProvider
.when('/', {
    templateUrl: 'Content/Partials/Home/Index.html'
})
.when('/Error', {
    templateUrl: 'Content/Partials/Shared/Error.html'
})
.otherwise({
    redirectTo: '/Error'
});

所以,如果我访问第一个链接,我的 URL 如下所示:

https://myapp.xyz/VirtualDirectory/#/

这可以完美地解决我的部分问题。 Angular 请求带有虚拟目录的部分,所以我看到一个 HTTP 请求:

https://myapp.xyz/VirtualDirectory/Content/Partials/Home/Index.html

但是,如果我在没有尾部斜杠的情况下访问:

https://myapp.xyz/VirtualDirectory

角度路线:

https://myapp.xyz/VirtualDirectory#/

一旦发生这种情况,我的路由都不起作用(它们不尊重虚拟目录)。他们的路线如下:

https://myapp.xyz/Content/Partials/Home/Index.html

这会破坏我在指令中定义的所有路由和模板。有关如何解决此问题的任何建议?

【问题讨论】:

  • 你的路线是如何定义的?
  • 我将编辑我的模板 URL 以显示完整的路线示例。

标签: asp.net-mvc angularjs iis


【解决方案1】:

尝试在你的 head 标签中添加这个。

<html>
<head>
    <base href="/VirtualDirectory/">
</head>
<body>
    ...
</body>

【讨论】:

  • 这不会改变任何东西。
  • 我撒谎了,这解决了问题。但是,我使用了:&lt;base href="@Url.Content("~")" /&gt;
  • 进一步补充,我实际上必须使用&lt;base href="@Url.Content("~/")" /&gt; 才能获得正确的基本 URL。
【解决方案2】:

我发现如果路径使用./语法,就不需要涉及Razor了。

失败的代码:

$routeProvider.when("/productList", {
            templateUrl: "/app/products/productListView.html", controller: "ProductListController as vm"
        });

有效的代码:(注意 /app 之前的 .)

$routeProvider.when("/productList", {
            templateUrl: "./app/products/productListView.html", controller: "ProductListController as vm"
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 2015-10-12
    相关资源
    最近更新 更多