I have a technical issue with the app.MapWhen()
method in .NET Core for handling routes in my application.
Context
I have two Angular v15 projects serving as front-ends for my .NET Core 6 application.
- The
back
project located inwwwroot/back
- The
survey
project located inwwwroot/survey
Goal
I want the application to load the survey
project when the URL is www.toto.com/survey
and to load the back
project for any other URL.
Configuration
Here is my current setup:
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.WebRootPath, "survey")),
RequestPath = new PathString("/survey")
});
app.UseFileServer(new FileServerOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.WebRootPath, "back")),
RequestPath = ""
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapFallback(context =>
{
if (context.Request.Path.StartsWithSegments("/survey"))
{
context.Response.ContentType = "text/html";
return context.Response.SendFileAsync(Path.Combine(env.WebRootPath, "survey", "index.html"));
}
// Serve the index.html from back folder for all other paths
context.Response.ContentType = "text/html";
return context.Response.SendFileAsync(Path.Combine(env.WebRootPath, "back", "index.html"));
});
});
Problem
Regardless of which project I try to load, if the URL contains a parameter (e.g., /survey
), the index.html
file is loaded correctly, but the JavaScript files are not. An error indicates that the wrong files are being loaded.
I only have the problem with /survey path, back works pretty well.
Question
How can I configure the routing and static file handling so that the correct JavaScript files are loaded for each project when their respective URLs are accessed?
Thank you for your help!
.MapWhen
, just use 2x static files learn.microsoft.com/en-us/aspnet/core/fundamentals/… Or just.MapWhen
the survey urls first.