我们正在努力将我们的应用程序迁移到新的 Angular 版本。旧的(Angular.JS)是它自己的应用程序/存储库,由 .NET Framework 4.5.2 ASP.NET MVC 应用程序提供服务,该应用程序会将每个调用路由到Home
这将返回一个包含 Angular 应用程序的 Razor 视图,然后启动它,可能会将 URL 的后缀路由到实际的 Angular 页面。例如,它将路由www.website.com/profile
to the profile
Angular 中的路由,而不是尝试访问名为的文件夹profile
在服务器上。
集成我们的 API 和 Angular.JS 应用程序虽然不太漂亮,但很有效。服务器还在 IIS 中具有虚拟文件夹,用于存放应访问的其他子文件夹,例如字体、图像、配置等。
目前我正在努力让 Angular 7 应用程序与 Angular.JS 应用程序一起运行。我们希望运行 Angular 7 应用程序以在当前应用程序的目录中运行,然后将其移动到它自己的 Web 应用程序,因为我们新的 Angular 应用程序只是一堆静态文件,不需要与服务器捆绑在一起不再了。基本上不需要任何 ASP.NET MVC 配置。
当前的网址将是www.website.com
并返回 Angular.JS 应用程序。新的将是www.website.com/v2/
并将返回 Angular 7 应用程序。如果我们完成了 v2 版本profile
页面,导航至wwww.website.com/v2/profile
现在应该导航到profile
Angular 7 应用程序的页面。
在 IIS 中,虚拟文件夹之一是名为的文件夹dist
这导致 Angular.JS 应用程序文件的位置。一个新的叫v2
现在已配置,这将导致 Angular 7 应用程序文件的位置。另一个叫assets
导致assets
Angular 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 应用程序上www.website.com
,即使如果{{anything}}
是 Angular 7 中的内部链接,它would导航至{{anything}}
route.
我使用以下命令构建 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',
},
];
谁能告诉我如何制作,例如打字www.website.com/v2/contact
浏览器中的 通向 v2 联系页面,同时还可以进行内部导航(因此,如果用户单击 Angular 7 应用程序内部的“导航到 v2 联系页面”,它也会导航到 v2 联系页面)
编辑:有关 Angular.JS 的信息Routerconfig
和后端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: '/'
});
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>