要在ASP.NET Core 2.1中提供多个Angular应用程序,您可以按照以下步骤操作:
在ASP.NET Core项目中创建多个Angular应用程序。可以使用Angular CLI分别创建这些应用程序。假设您有两个Angular应用程序,分别命名为“app1”和“app2”。
在ASP.NET Core项目的根文件夹中创建两个文件夹,分别命名为“app1”和“app2”。将“app1”和“app2”文件夹中的所有内容复制到相应的文件夹中。
打开“Startup.cs”文件,并添加以下代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// 添加Angular应用程序1
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "app1/dist";
});
// 添加Angular应用程序2
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "app2/dist";
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseMvc();
app.UseSpa(spa =>
{
// 配置Angular应用程序1
spa.Options.SourcePath = "app1";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
// 配置Angular应用程序2
spa.Options.SourcePath = "app2";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
}
"scripts": {
"start:app1": "ng serve --open --port 4201",
"start:app2": "ng serve --open --port 4202"
}
npm run start:app1
npm run start:app2
这样,您就可以在ASP.NET Core项目中同时提供多个Angular应用程序了。每个应用程序都可以在不同的端口上运行,例如“http://localhost:4201”和“http://localhost:4202”。