【问题标题】:.Net Core with Angular 4 : 404 error when I refresh the browser.Net Core with Angular 4:刷新浏览器时出现404错误
【发布时间】:2018-08-28 22:30:21
【问题描述】:

我已经使用 .Net core 2.0、visual studio 2017 构建了一个 angular 4 应用程序,并部署到 IIS Web 服务器上的生产服务器。刷新时路由在本地主机上工作正常,但不能在现场工作。我在 web.config 文件中的 IIS 的 stackoverflow 中看到了一些答案。但是我的项目结构下没有 web.config 文件。我可以手动添加它还是如何修复它。

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

这是我的 app.routing 文件

   const appRoutes: Routes = [
  { path: '', redirectTo: 'admin-dashboard', pathMatch: 'full' },
  { path: 'login', component: LoginComponent },
  { path: 'forgotpassword', component: ForgotPasswordComponent },
  { path: 'user-dashboard', component: DashboardComponent, canActivate: [AuthGuard] },
 { path: 'notifications', component: NotificationsComponent, canActivate: [AuthGuard] },
 {
path: 'admin-dashboard', component: AdminDashboardComponent, canActivate: [AuthGuard],
children: [
  { path: '', component: ChairmanMessagesComponent, canActivate: [AuthGuard]},
  { path: 'quotes', component: QuotesComponent, canActivate: [AuthGuard] },
  { path: 'users', component: UsersComponent, canActivate: [AuthGuard] },
  { path: 'notifications', component: AdminNotificationsComponent, canActivate: [AuthGuard] },
]

}, ];

 export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

请告诉我。

【问题讨论】:

  • 嗨..我有同样的问题..你解决了吗?

标签: angular asp.net-core-2.0


【解决方案1】:

路由应用程序必须回退到 index.html,因此请查看此link 并为 IIS 添加配置(如果您正在使用该配置),或者在此处编辑您的配置

<action type="Rewrite" url="/index.html" />

【讨论】:

    【解决方案2】:

    在后端(服务器端)只需将所有 (404) 错误/无法识别的请求映射到您的索引(包括所有角度构建脚本)。然后它返回索引。在前端,你可以得到你需要的。它映射到你需要的页面。

    例如:- In spring Boot back-end this is an example answer

    【讨论】:

      【解决方案3】:

      在以下路径上刷新浏览器时我遇到了同样的问题:/users 我通过创建一个名为的文件夹来修复它:用户 并在文件中:index.html

      <!<!DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8" />
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <title>redirect users</title>
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <script>
              window.location.href = 'http://my-server:4200/';
          </script>
      </head>
      <body>    
      </body>
      </html>
      

      我读过的其他解决方案:

      1) 让您的服务器在出现 404 错误时重定向到 index.html。

      2) 让您的 apache 或 iis 在每个路由路径的末尾添加 /index.html

      【讨论】:

      • 如果在 Angular 5.0.0 bennadel.com/blog/…987654321@ 中刷新页面,则阻止路由到辅助视图
      猜你喜欢
      • 2016-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      相关资源
      最近更新 更多