我一直在学习Angular2。在nodejs lite-server上运行时,路由工作正常。我可以转到主(localhost:3000)页面并浏览应用程序。我也可以输入localhost:3000 / employee,它将转到请求的页面。
该应用最终将存在于Windows IIS 7.5服务器上。如果我从主页面(localhost:2500)开始,路由工作正常,我可以在没有任何问题的情况下通过应用程序。我遇到问题的地方是尝试直接转到主登陆页面以外的页面(localhost:2500 / employee)。我收到HTTP错误404.0。
这是我的app.component文件,用于处理路由。
import { Component } from '@angular/core';
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router-deprecated';
import { HTTP_PROVIDERS } from '@angular/http';
import { UserService } from './services/users/user.service';
import { LogonComponent } from './components/logon/logon.component';
import { EmployeeComponent } from './components/employee/employee.component';
@Component({
selector : 'opi-app',
templateUrl : 'app/app.component.html',
directives: [ROUTER_DIRECTIVES],
providers: [ROUTER_PROVIDERS, UserService, HTTP_PROVIDERS]
})
@RouteConfig([
{
path: '/',
name: 'Logon',
component: LogonComponent,
useAsDefault: true
},
{
path: '/employee',
name: 'Employee',
component: EmployeeComponent
}
])
export class AppComponent { }
我想说我理解这个问题。 IIS正在寻找/ employee的目标,但它不存在。我只是不知道如何使IIS知道路由。
答案 0 :(得分:56)
(对于角度2,角度4)
按照上述文章创建一个web.config文件。
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="AngularJS Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
将其放在您网站的根目录(与index.html相同)中。我的是dist文件夹(angular-cli项目)。
部署后,如果您具有访问权限,请转到IIS,然后单击重写规则图标并查看它是否读取此配置。如果您没有看到重写规则的图标,则需要在“modules”图标下启用该模块。这段代码片段不是由我编写的,但我想分享更好的实现细节。
答案 1 :(得分:8)
您应该使用URL rewrite module来实现此目的。 here
提供了有关如何使用它的详细信息在IIS 8上为我工作的示例web.config片段是here。
答案 2 :(得分:1)
使用URL重写模块将终止应用程序内的路由路径。我已将404 Not Found响应的错误页面更改为我的index.html
文件。这不会更改浏览器中的URL,但服务器会返回整个应用程序。
HowTo:Site-&gt; Application-&gt;错误状态代码404上下文菜单中的页面选择编辑...并选择此站点上的选项执行URL。输入/index.html
,然后按确定。请注意,路径来自站点根目录,因此您可能需要包含应用路径。
答案 3 :(得分:1)
我的角度6应用程序正在子文件夹中运行,我遇到了重定向到子文件夹的问题。相反,我直接重定向到index.html
步骤1.)使用--base-href
标记构建角度应用,如下所示:ng build --prod --base-href /ng/
步骤2.)在此文件夹中创建一个web.config,其中包含对index.html的redirct,如下所示:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="./index.html" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
答案 4 :(得分:0)
如果您使用Core 2.1项目,则可以通过将这段代码放在Startup.cs
的底部来跳过web.config重写规则并完成Angular深度链接。
app.Use(async (context, next) =>
{
context.Response.ContentType = "text/html";
await context.Response.SendFileAsync(Path.Combine(env.WebRootPath, "index.html"));
});
答案 5 :(得分:0)
如果您使用的不是应用程序的root,则可以尝试以下操作:
<rule name="AngularJS Routes" stopProcessing="true">
<match url="(.*)mypath/myangularapp/dashboard(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/mypath/myangularapp/dashboard/index.html" />
</rule>
确保通过以下命令构建了Angular应用程序:
ng build --base-href=/mypath/myangularapp/dashboard/