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:
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?
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.
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.
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/">
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";
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With