Skip to content

Commit 7d72a78

Browse files
claudiamurialdoclaudiamurialdo
andauthored
Change fix of #1179: Use application convention to conditionally register HomeController only when a default file exists, ensuring REST controllers are properly loaded. (#1183)
Co-authored-by: claudiamurialdo <c.murialdo@globant.com>
1 parent 009bf03 commit 7d72a78

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,12 @@ public void ConfigureServices(IServiceCollection services)
194194
{
195195
OpenTelemetryService.Setup(services);
196196

197-
IMvcBuilder builder = services.AddMvc(option => option.EnableEndpointRouting = false);
198-
197+
IMvcBuilder builder = services.AddMvc(option =>
198+
{
199+
option.EnableEndpointRouting = false;
200+
option.Conventions.Add(new HomeControllerConvention());
201+
});
202+
199203
RegisterControllerAssemblies(builder);
200204

201205
services.Configure<KestrelServerOptions>(options =>
@@ -561,7 +565,10 @@ public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHos
561565
app.UseHttpsRedirection();
562566
app.UseHsts();
563567
}
564-
568+
app.UseEndpoints(endpoints =>
569+
{
570+
endpoints.MapControllers();
571+
});
565572
if (log.IsCriticalEnabled && env.IsDevelopment())
566573
{
567574
try
@@ -648,13 +655,6 @@ public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHos
648655
routes.MapRoute($"{restBasePath}{{*{UrlTemplateControllerWithParms}}}", new RequestDelegate(gxRouting.ProcessRestRequest));
649656
});
650657
}
651-
if (FindAndStoreDefaultFile())
652-
{
653-
app.UseEndpoints(endpoints =>
654-
{
655-
endpoints.MapControllerRoute("Default", VirtualPath, new { controller = "Home", action = "Index" });
656-
});
657-
}
658658

659659
app.UseWebSockets();
660660
string basePath = string.IsNullOrEmpty(VirtualPath) ? string.Empty : $"/{VirtualPath}";
@@ -677,21 +677,6 @@ private void ConfigureCors(IApplicationBuilder app)
677677
app.UseCors(CORS_POLICY_NAME);
678678
}
679679
}
680-
private static bool FindAndStoreDefaultFile()
681-
{
682-
string[] defaultFiles = { "default.htm", "default.html", "index.htm", "index.html" };
683-
foreach (string file in defaultFiles)
684-
{
685-
string filePath = Path.Combine(LocalPath, file);
686-
if (File.Exists(filePath))
687-
{
688-
DefaultFileName = file;
689-
return true;
690-
}
691-
}
692-
DefaultFileName = null;
693-
return false;
694-
}
695680

696681
private void ConfigureSwaggerUI(IApplicationBuilder app, string baseVirtualPath)
697682
{
@@ -853,4 +838,35 @@ public void Apply(ApplicationModel application)
853838
}
854839
}
855840
}
841+
842+
internal class HomeControllerConvention : IApplicationModelConvention
843+
{
844+
private static bool FindAndStoreDefaultFile()
845+
{
846+
string[] defaultFiles = { "default.htm", "default.html", "index.htm", "index.html" };
847+
foreach (string file in defaultFiles)
848+
{
849+
string filePath = Path.Combine(Startup.LocalPath, file);
850+
if (File.Exists(filePath))
851+
{
852+
Startup.DefaultFileName = file;
853+
return true;
854+
}
855+
}
856+
Startup.DefaultFileName = null;
857+
return false;
858+
}
859+
860+
public void Apply(ApplicationModel application)
861+
{
862+
var homeController = application.Controllers.FirstOrDefault(c => c.ControllerType == typeof(HomeController));
863+
if (homeController != null)
864+
{
865+
if (!FindAndStoreDefaultFile())
866+
{
867+
application.Controllers.Remove(homeController);
868+
}
869+
}
870+
}
871+
}
856872
}

0 commit comments

Comments
 (0)