如何在ASP.NET Core中处理多个SPA应用程序 [英] How to handle multiple SPA application in ASP.NET Core

查看:315
本文介绍了如何在ASP.NET Core中处理多个SPA应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ASP.NET Core Web API应用程序,我想提供两个Angular应用程序,一个用于管理员,一个用于用户.

I have an ASP.NET Core Web API application, I want to serve two Angular apps, one for admin and one for users.

在生产中,我不使用角度CLI工具,因此只有两个角度Web应用程序的一些静态文件.

In production, I don't use angular CLI tools so there is only some static files of two angular web applications.

管理文件在/angular/admin中,用户文件在/angular/user中. 那么,我该如何为他们服务?

Admin files are in /angular/admin and user files are in /angular/user. So, how can I serve them?

我尝试了多次对IServiceCollection.AddSpaStaticFiles的调用,但它们相互覆盖. [从源存储库中,我发现这是Singleton服务]

I tried multiple calls to IServiceCollection.AddSpaStaticFiles but they override each other. [From source repository I found this is Singleton services]

推荐答案

设置MVC后,您必须将应用程序中间件管道分成两个分支并注册SPA

You have to branch the application middleware pipeline into two and register the SPAs after setting up MVC

 ...
 app.UseMvc(...)

 app.Map("/admin",
   adminApp =>
   {
     adminApp.UseSpa(spa =>
     {
       spa.Options.SourcePath = "angular/admin";
       spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
       {
           FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "angular", "admin"))
       };

       if (env.IsDevelopment())
         spa.UseProxyToSpaDevelopmentServer("http://localhost:4200");
      });
    });

  app.Map("/user",
    userApp =>
    {
      userApp.UseSpa(spa =>
      {
        spa.Options.SourcePath = "angular/user";
        spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "angular", "user"))
        };

        if (env.IsDevelopment())
          spa.UseProxyToSpaDevelopmentServer("http://localhost:4201");
      });
  });  

                ```

这篇关于如何在ASP.NET Core中处理多个SPA应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆