Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 41 additions & 25 deletions dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,12 @@ public void ConfigureServices(IServiceCollection services)
{
OpenTelemetryService.Setup(services);

IMvcBuilder builder = services.AddMvc(option => option.EnableEndpointRouting = false);

IMvcBuilder builder = services.AddMvc(option =>
{
option.EnableEndpointRouting = false;
option.Conventions.Add(new HomeControllerConvention());
});

RegisterControllerAssemblies(builder);

services.Configure<KestrelServerOptions>(options =>
Expand Down Expand Up @@ -561,7 +565,10 @@ public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHos
app.UseHttpsRedirection();
app.UseHsts();
}

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
if (log.IsCriticalEnabled && env.IsDevelopment())
{
try
Expand Down Expand Up @@ -648,13 +655,6 @@ public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHos
routes.MapRoute($"{restBasePath}{{*{UrlTemplateControllerWithParms}}}", new RequestDelegate(gxRouting.ProcessRestRequest));
});
}
if (FindAndStoreDefaultFile())
{
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("Default", VirtualPath, new { controller = "Home", action = "Index" });
});
}

app.UseWebSockets();
string basePath = string.IsNullOrEmpty(VirtualPath) ? string.Empty : $"/{VirtualPath}";
Expand All @@ -677,21 +677,6 @@ 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)
{
Expand Down Expand Up @@ -853,4 +838,35 @@ public void Apply(ApplicationModel application)
}
}
}

internal class HomeControllerConvention : IApplicationModelConvention
{
private static bool FindAndStoreDefaultFile()
{
string[] defaultFiles = { "default.htm", "default.html", "index.htm", "index.html" };
foreach (string file in defaultFiles)
{
string filePath = Path.Combine(Startup.LocalPath, file);
if (File.Exists(filePath))
{
Startup.DefaultFileName = file;
return true;
}
}
Startup.DefaultFileName = null;
return false;
}

public void Apply(ApplicationModel application)
{
var homeController = application.Controllers.FirstOrDefault(c => c.ControllerType == typeof(HomeController));
if (homeController != null)
{
if (!FindAndStoreDefaultFile())
{
application.Controllers.Remove(homeController);
}
}
}
}
}
Loading