【问题标题】:Manage two SPA in a single ASP.NET Core 3.1 Web API; One in the root and the other in "admin" route在单个 ASP.NET Core 3.1 Web API 中管理两个 SPA;一个在根目录中,另一个在“管理员”路由中
【发布时间】:2021-10-04 18:04:08
【问题描述】:

我有这个被 2 个 SPA Angular 应用程序使用的 ASP.NET Core 3.1 Web API 应用程序;一个在“app”路由中,另一个在“admin”路由中。

路由代码如下:

app.Map("/app", client => // =======> changed to "/" but no used
            {
                client.UseSpa(spa =>
                {
                spa.Options.SourcePath = "app";
                spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
                {
                    FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "app")),
                    
                };
                });
            }).Map("/admin", admin =>
            {
                admin.UseSpa(spa =>
                {
                    spa.Options.SourcePath = "admin";
                    spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
                    {
                        FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "admin")),
                      
                    };
                });
            });

我需要转到根目录中的第一个应用程序(app)。我尝试将路由“/app”更改为“/”,但没有成功。

【问题讨论】:

  • 将管理员app.UseSpa 移至app.Map("/admin") 之后的应用部分。所以它应该看起来像app.Map("/admin", ...); app.UseSpa(/* config for app */)

标签: c# angular asp.net-core


【解决方案1】:

要以根目录服务主应用程序,请从 .Map() 分支中删除其 SPA 中间件,并将其移至管理应用程序的中间件之后。

// ...
app.Map("/admin", adminApp => 
    adminApp.UseSpa(spa => {
        spa.Options.SourcePath = "admin";
        // ...
    })
);

app.UseSpa(spa => {
    spa.Options.SourcePath = "app";
    // ...
});
// ...

所以当对/admin* 的请求到来时,它将跟随.Map("/admin") 创建的分支并由管理员SPA 中间件提供服务。 对于所有其他请求,将执行主分支,并为主应用程序提供服务。

【讨论】:

  • 我的 Angular 项目呢?我应该使用 basehref="/app/" 来构建它吗?
猜你喜欢
  • 1970-01-01
  • 2018-03-15
  • 2021-05-01
  • 2020-07-20
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 2017-01-28
  • 1970-01-01
相关资源
最近更新 更多