From b8dfcaad7818ed5506973fb614a65b3bb9832f8a Mon Sep 17 00:00:00 2001 From: claudiamurialdo Date: Mon, 8 Sep 2025 13:52:28 -0300 Subject: [PATCH] Add conditional default route registration and static file detection for HomeController in Startup. --- .../dotnetcore/GxNetCoreStartup/Startup.cs | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs b/dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs index cfdbe3f44..5101b1105 100644 --- a/dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs +++ b/dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs @@ -174,6 +174,7 @@ public class Startup const string CORS_ANY_ORIGIN = "*"; const double CORS_MAX_AGE_SECONDS = 86400; internal const string GX_CONTROLLERS = "gxcontrollers"; + internal static string DefaultFileName { get; set; } public List servicesBase = new List(); @@ -530,10 +531,6 @@ public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHos app.UseHttpsRedirection(); app.UseHsts(); } - app.UseEndpoints(endpoints => - { - endpoints.MapControllers(); - }); if (log.IsCriticalEnabled && env.IsDevelopment()) { @@ -621,10 +618,13 @@ public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHos routes.MapRoute($"{restBasePath}{{*{UrlTemplateControllerWithParms}}}", new RequestDelegate(gxRouting.ProcessRestRequest)); }); } - app.UseMvc(routes => + if (FindAndStoreDefaultFile()) { - routes.MapRoute("Default", VirtualPath, new { controller = "Home", action = "Index" }); - }); + app.UseEndpoints(endpoints => + { + endpoints.MapControllerRoute("Default", VirtualPath, new { controller = "Home", action = "Index" }); + }); + } app.UseWebSockets(); string basePath = string.IsNullOrEmpty(VirtualPath) ? string.Empty : $"/{VirtualPath}"; @@ -647,6 +647,21 @@ private void ConfigureCors(IApplicationBuilder app) app.UseCors(CORS_POLICY_NAME); } } + private static bool FindAndStoreDefaultFile() + { + string[] defaultFiles = { "default.htm", "default.html", "index.htm", "index.html" }; + foreach (string file in defaultFiles) + { + string filePath = Path.Combine(LocalPath, file); + if (File.Exists(filePath)) + { + DefaultFileName = file; + return true; + } + } + DefaultFileName = null; + return false; + } private void ConfigureSwaggerUI(IApplicationBuilder app, string baseVirtualPath) { @@ -762,13 +777,11 @@ public class HomeController : Controller { public IActionResult Index() { - string[] defaultFiles = { "default.htm", "default.html", "index.htm", "index.html" }; - foreach (string file in defaultFiles) { - if (System.IO.File.Exists(Path.Combine(Startup.LocalPath, file))){ - return Redirect(Url.Content($"~/{file}")); - } + if (!string.IsNullOrEmpty(Startup.DefaultFileName)) + { + return Redirect(Url.Content($"~/{Startup.DefaultFileName}")); } - return Redirect(defaultFiles[0]); + return NotFound(); } } internal class SetRoutePrefix : IApplicationModelConvention