Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net core 2.1 - How to serve multiple angular apps?

I'm trying to serve 2 angular apps from my .net core service like this:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "wwwroot/app";
    });

    services.AddSpaStaticFiles(configuration =>
    {
        configuration.RootPath = "wwwroot/admin";
    });

    ...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...

    app.UseSpaStaticFiles(new StaticFileOptions
    {
        OnPrepareResponse = ctx =>
        {
            ctx.Context.Response.Headers.Append("Cache-Control", "public,max-age=0");
        }
    });

    app.UseMvc();

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

in the file system, I only have the dist output of these apps (since they are developed by another team). So it looks like this:

  • C:[MY PROJECT PATH]\wwwroot\app
  • C:[MY PROJECT PATH]\wwwroot\admin

But for some reason, the admin app works and the app doesn't and also it doesn't support a default page so I need to enter the URL with /index.html.

Any ideas on how to solve this?

like image 983
Liran Friedman Avatar asked Mar 20 '19 08:03

Liran Friedman


People also ask

How do I add an Angular project to an existing .NET core Web API project?

To use publish, create your JavaScript project using Visual Studio 2022 version 17.3 or later. In Solution Explorer, right-click the ASP.NET Core project and choose Add > Project Reference. Select the Angular project and choose OK. Right-click the ASP.NET Core project in Solution Explorer and choose Unload Project.

Can we use Angular with ASP NET core?

The updated Angular project template provides a convenient starting point for ASP.NET Core apps using Angular and the Angular CLI to implement a rich, client-side user interface (UI). The template is equivalent to creating an ASP.NET Core project to act as an API backend and an Angular CLI project to act as a UI.


2 Answers

Well, I finally solved it and it works like this:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddSpaStaticFiles();

    ...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...

    app.UseSpaStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
    });

    app.UseMvc();

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

Also, don't forget to go into the index.html file of each app and set the base tag accordingly like this:

//for the client application:
<base href="/app/">

//for the admin application
<base href="/admin/">
like image 79
Liran Friedman Avatar answered Oct 23 '22 13:10

Liran Friedman


Liran answered his own question. In my case I had multiple entry points for a vue cli site. The fix is identical to the correct answer, but also setting spa.Options.DefaultPage.

    app
         .Map("/login", admin =>
         {
             admin.UseSpa(spa =>
             {
                 spa.Options.SourcePath = "ClientApp";
                 spa.Options.DefaultPage = "/login.html";
             });
         })
        .Map("", client =>
    {
        client.UseSpa(spa =>
        {
            spa.Options.SourcePath = "ClientApp";
            spa.Options.DefaultPage = "/index.html";
        });
    });
like image 34
MGDavies Avatar answered Oct 23 '22 14:10

MGDavies