首页 文章

Angular 6路由使用ng服务,而不是Visual Studio

提问于
浏览
1

我觉得这是一个愚蠢的问题,但我是Angular的新手,无法在任何地方找到答案......

我正在尝试在新的Angular 6应用程序中设置路由 . 当我使用ng serve从命令行提供服务时,我可以路由到组件就好了,但是当我从Visual Studio提供服务时却没有 . 这只是基本的路由 .

在app-routing.module.ts中定义的路由:

const routes: Routes = [
  { path: '', component: MaintenanceComponent },
  { path: 'merchants', component: MaintenanceComponent }  
];

app.component.html中的router-outlet标记:

<div style="text-align:center">
  <h1>
    Welcome to {{ title }}!
  </h1>
</div>
<router-outlet></router-outlet>

有趣的是,我可以告诉路由器在VS中运行时识别对root的请求,因为它将返回404而没有{path:'',component:MaintenanceComponent} . 然而,试图访问/商家返回404无论如何 . 这是一个线索 .

我错过了什么简单的事情?

该项目使用.NET Core Web API .

1 回答

  • 1

    这是因为当客户端请求/商家时,服务器需要返回index.html以启动角度路由 . 这样做的方法是通过添加到web.config来重写IIS URL:

    <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="/" />
          </rule>
        </rules>
      </rewrite>
    </system.webServer>
    

    这告诉iis将所有无效文件或目录的请求重写到/ url

相关问题