在子文件夹中运行的 Angular 7 中的路由无法正常工作

S. *_*nke 3 javascript asp.net iis angularjs angular

我们正在努力将我们的应用程序迁移到新的 Angular 版本。旧的(Angular.JS)是它自己的应用程序/存储库由 .NET Framework 4.5.2 ASP.NET MVC 应用程序提供服务,该应用程序将路由每个调用,Home该调用将返回包含角度应用程序的 Razor 视图,然后启动它向上,可能将 URL 的后缀路由到实际的 Angular 页面。例如,它会路由www.website.com/profileprofileAngular 中的路由,而不是尝试访问profile服务器上调用的文件夹。集成我们的 API 和 Angular.JS 应用程序并不太漂亮,但它奏效了。服务器在 IIS 中还有虚拟文件夹,用于其他应该访问的子文件夹,如字体、图像、配置等。

目前我正在努力让 Angular 7 应用程序与 Angular.JS 应用程序一起运行。我们希望运行 Angular 7 应用程序以在当前应用程序的目录中运行,然后将其移动到它自己的 Web 应用程序中,因为我们的新 Angular 应用程序只是一堆静态文件,不需要与服务器捆绑了。基本上,不需要任何 ASP.NET MVC 配置。

当前 url 将www.website.com返回 Angular.JS 应用程序。新的www.website.com/v2/将返回 Angular 7 应用程序。如果我们已经完成了profile页面的 v2 版本,导航到wwww.website.com/v2/profile现在应该导航到profileAngular 7 应用程序的页面。

在 IIS 中,其中一个虚拟文件夹是一个名为的文件夹dist,它指向 Angular.JS 应用程序文件的位置。v2现在配置了一个名为的新文件,它指向 Angular 7 应用程序文件的位置。另一个称为assets导致assetsAngular 7 应用程序的文件夹。

现在,当我导航到 时www.website.com/v2/,Angular 启动并将我路由到www.website.com/v2/home,这是主路由。

问题是当我www.website.com/v2/home自己输入时,它会导致应用程序路由回www.website.com. 因此它会再次启动 Angular.JS 应用程序。没有发生错误或重定向。似乎它只是忽略了部分之后的v2/部分,即使 Angular.JS 做得很好。

TL;DR导航到www.website.com/v2加载 Angular 7,但导航到v2/{{Anything}}导致应用程序回退到运行于 Angular.JS 应用程序的 Angular.JS 应用程序www.website.com,即使它{{anything}}是 Angular 7 中的内部链接,它也会导航到{{anything}}路由。

我使用以下命令构建 Angular 项目:

ng build --configuration=dev --deploy-url=/v2/ --base-href=/v2/

angular 7 应用程序中的路由如下所示:

const routes: Routes = [
  {
    path: '',
    redirectTo: 'home',
    pathMatch: 'full',
  },
  {
    path: '',
    canActivate: [AuthGuard],
    component: DefaultLayoutComponent,
    children: [
      {
        path: 'home',
        component: DashboardComponent,
        canLoad: [NoAuthGuard],
      },
      {
        path: 'contact',
        loadChildren: './modules/contact/contact.module#ContactModule',
        canLoad: [NoAuthGuard],
      },
      {
        path: 'timesheets',
        loadChildren: './modules/timesheet/timesheet.module#TimesheetModule',
        canLoad: [NoAuthGuard],
      },
      {
        path: 'users',
        loadChildren: './modules/users/users.module#UsersModule',
        canLoad: [NoAuthGuard, NgxPermissionsGuard],
        data: {
          permissions: {
            only: 'seeUsersPage',
            redirectTo: '/403',
          },
        },
      },
      {
        path: 'profile',
        loadChildren: './modules/profile/profile.module#ProfileModule',
        canLoad: [NoAuthGuard],
      },
    ],
  },
  {
    path: 'login',
    component: LoginComponent,
  },
  {
    path: '403',
    component: Page403Component,
  },
  {
    path: '**',
    redirectTo: '/home',
    pathMatch: 'full',
  },
];
Run Code Online (Sandbox Code Playgroud)

谁能告诉我,例如,我可以如何www.website.com/v2/contact在浏览器中输入指向 v2 联系页面,同时还可以进行内部导航(因此,如果用户单击 Angular 7 内部的“导航到 v2 联系页面”应用程序,它还导航到 v2 联系页面)

编辑:请求有关 Angular.JSRouterconfig和后端的信息web.config;我在下面提供了它们。

RouterConfig

$routeProvider
    .when('/', {
      templateUrl: 'scripts/home/home.html',
      controller: 'HomeController',
      controllerAs: 'homeCtrl'
    })
    .when('/gebruikers', {
      templateUrl: 'scripts/users/users.html',
      controller: 'UsersController',
      controllerAs: 'usersCtrl'
    })
    .when('/profiel', {
      templateUrl: 'scripts/profile/profile.html',
      controller: 'ProfileController',
      controllerAs: 'profileCtrl'
    })
    .when('/timesheets', {
      templateUrl: 'scripts/timesheets/timesheets.html',
      controller: 'TimesheetsController',
      controllerAs: 'timesheetsCtrl',
      reloadOnSearch: false
    })
    .when('/facturen', {
      templateUrl: 'scripts/invoices/invoices.html',
      controller: 'InvoicesController',
      controllerAs: 'invoicesCtrl',
      reloadOnSearch: false
    })
    .when('/opdrachten', {
      templateUrl: 'scripts/vacancies/vacancies.html',
      controller: 'VacanciesController',
      controllerAs: 'vacanciesCtrl'
    })
    .when('/contact', {
      templateUrl: 'scripts/contact/contact.html',
      controller: 'ContactController',
      controllerAs: 'contactCtrl'
    })
    .when('/help', {
      templateUrl: 'scripts/help/help.html',
      controller: 'HelpController',
      controllerAs: 'helpCtrl'
    })
    .otherwise({
      redirectTo: '/'
    });
Run Code Online (Sandbox Code Playgroud)

Web.Config IIS rewrite rules:

 <rewrite>
      <rules>
        <clear />
        <rule name="Redirect Angular endpoints to MVC Home" enabled="true">
          <match url="^(profiel|timesheets|facturen|opdrachten|contact|help|gebruikers)" negate="false" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
          <action type="Rewrite" url="Home" logRewrittenUrl="false" />
        </rule>
     <!-- Some other rules that are not important are here, they redirect HTTP to HTTPS -->
     </rules>
  </rewrite>
Run Code Online (Sandbox Code Playgroud)

S. *_*nke 5

它总是路由回 v1 url 的原因是因为 angular 7 应用程序包含一个web.config看起来像这样的文件:

<configuration>
    <system.webServer>
      <rewrite>
        <rules>
          <rule name="Angular" 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>
Run Code Online (Sandbox Code Playgroud)

我相信这是 IIS 部署的默认设置,并且团队成员可能已经添加了它。我从来没有想过在前端项目中有一个 web.config。

基本上,这条规则负责将用户重新路由到 index.html。例如www.website.com/folder,通常会将用户带到folderWeb 服务器上调用的文件夹,但这不是 Angular 运行的地方,可能会导致 404。此规则将用户带回www.website.com,然后让Angular路由folder部件。

因为我在子文件夹中运行我的应用程序,所以我将其更改url="/"url="/v2/"并且它立即起作用了!

记得用ng build --configuration=dev --deploy-url=/v2/ --base-href=/v2/! 需要deploy-url,因为在服务器的根文件夹中没有找到scripts/css/other文件;它位于 /v2/ 中。路由器需要base-href;它应该开始路由应用程序,www.website.com/v2/而不是在www.website.com/

不需要其他 iis 重写规则,不需要 asp.net 配置,不需要特殊的角度路由。只需这几个简单的步骤!