IIS 8.x虚拟目录上的Angular应用程序具有奇怪的URL行为

Ada*_*lin 12 iis asp.net-mvc angularjs

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

https://myapp.xyz/VirtualDirectory/
Run Code Online (Sandbox Code Playgroud)

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

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

所以,如果我访问第一个链接,我的URL看起来像:

https://myapp.xyz/VirtualDirectory/#/
Run Code Online (Sandbox Code Playgroud)

这完美地工作,并解决了我的偏好.Angular请求部分包含虚拟目录,因此我看到一个HTTP请求:

https://myapp.xyz/VirtualDirectory/Content/Partials/Home/Index.html
Run Code Online (Sandbox Code Playgroud)

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

https://myapp.xyz/VirtualDirectory
Run Code Online (Sandbox Code Playgroud)

Angular航线:

https://myapp.xyz/VirtualDirectory#/
Run Code Online (Sandbox Code Playgroud)

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

https://myapp.xyz/Content/Partials/Home/Index.html
Run Code Online (Sandbox Code Playgroud)

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

小智 14

尝试在头标记中添加此内容.

<html>
<head>
    <base href="/VirtualDirectory/">
</head>
<body>
    ...
</body>
Run Code Online (Sandbox Code Playgroud)

  • 我骗了,这解决了这个问题.但是,我使用了:`<base href ="@ Url.Content("〜")"/>` (6认同)
  • 为了进一步添加,我实际上必须使用`<base href ="@ Url.Content("〜/")"/>`才能获得正确的基本URL. (3认同)