This commit is contained in:
2026-07-24 12:47:12 +08:00 Unverified
parent 67905dfa16
commit 6c3021ea8d
6 changed files with 125 additions and 13 deletions
+42
View File
@@ -182,6 +182,25 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI();
}
app.UseDefaultFiles();
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = context =>
{
if (context.Context.Request.Path.StartsWithSegments("/assets"))
{
context.Context.Response.Headers.CacheControl =
"public,max-age=31536000,immutable";
}
else if (string.Equals(
Path.GetExtension(context.File.Name),
".html",
StringComparison.OrdinalIgnoreCase))
{
context.Context.Response.Headers.CacheControl = "no-cache,no-store";
}
}
});
app.UseCors("Web");
app.UseAuthentication();
app.UseAuthorization();
@@ -194,6 +213,29 @@ app.MapGet("/health", () => Results.Ok(new
Environment = app.Environment.EnvironmentName,
Time = DateTimeOffset.UtcNow
})).AllowAnonymous();
app.MapFallback(async context =>
{
if (context.Request.Path.StartsWithSegments("/api") ||
context.Request.Path.StartsWithSegments("/health") ||
context.Request.Path.StartsWithSegments("/swagger"))
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
var webRoot = app.Environment.WebRootPath
?? Path.Combine(app.Environment.ContentRootPath, "wwwroot");
var indexPath = Path.Combine(webRoot, "index.html");
if (!File.Exists(indexPath))
{
context.Response.StatusCode = StatusCodes.Status404NotFound;
return;
}
context.Response.Headers.CacheControl = "no-cache,no-store";
context.Response.ContentType = "text/html; charset=utf-8";
await context.Response.SendFileAsync(indexPath);
});
using (var scope = app.Services.CreateScope())
{