feat: Enhance static file serving and routing for development and production environments #3

This commit is contained in:
Marek Lesko
2025-07-27 16:40:13 +00:00
parent b991f009c7
commit af3b0aefe4
4 changed files with 47 additions and 3 deletions

3
Api/.gitignore vendored
View File

@@ -1,4 +1,5 @@
obj/
bin/
.vs/
build/
build/
**/core

View File

@@ -56,6 +56,51 @@ namespace Api
app.UseHttpsRedirection();
}
if (app.Environment.IsDevelopment())
{
var staticFilePath = "/workspaces/centrum/Web/dist/Web/browser";
app.UseDefaultFiles(new DefaultFilesOptions
{
FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(staticFilePath),
DefaultFileNames = new List<string> { "index.html" }
});
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(staticFilePath),
RequestPath = ""
});
// Angular routing fallback
app.Use(async (context, next) =>
{
await next();
var path = context.Request.Path.Value ?? string.Empty;
if (context.Response.StatusCode == 404 &&
!System.IO.Path.HasExtension(path) &&
!path.StartsWith("/api"))
{
context.Request.Path = "/index.html";
await next();
}
});
}
else
{
app.UseDefaultFiles(); // Uses wwwroot by default
app.UseStaticFiles();
// Angular routing fallback for production
app.Use(async (context, next) =>
{
await next();
var path = context.Request.Path.Value ?? string.Empty;
if (context.Response.StatusCode == 404 &&
!System.IO.Path.HasExtension(path) &&
!path.StartsWith("/api"))
{
context.Request.Path = "/index.html";
await next();
}
});
}
app.UseAuthentication();
app.UseAuthorization();

BIN
Api/core

Binary file not shown.