From 78809c0a4d107c44e8c4ab5ba0a053995374e5b6 Mon Sep 17 00:00:00 2001 From: Tommy Date: Fri, 4 Oct 2024 14:37:05 +0200 Subject: [PATCH] UsePathBase instead of adding the base address to each route registration --- src/Dibix.Http.Host/Program.cs | 7 +++++++ .../Registration/DefaultEndpointRegistrar.cs | 6 +----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Dibix.Http.Host/Program.cs b/src/Dibix.Http.Host/Program.cs index b854cb87..9d1b9e63 100644 --- a/src/Dibix.Http.Host/Program.cs +++ b/src/Dibix.Http.Host/Program.cs @@ -123,6 +123,13 @@ void ConfigureLogging(ILoggingBuilder logging) WebApplication app = builder.Build(); + ILogger logger = loggerFactory.CreateLogger($"Dibix.Http.Host.{nameof(Program)}"); + logger.LogInformation("Using path base: {pathBase}", !String.IsNullOrEmpty(hostingOptions.BaseAddress) ? hostingOptions.BaseAddress : "/"); + + if (!String.IsNullOrWhiteSpace(hostingOptions.BaseAddress)) + { + app.UsePathBase(hostingOptions.BaseAddress); + } app.UseMiddleware(); app.UseExceptionHandler(); app.UseRouting(); diff --git a/src/Dibix.Http.Host/Registration/DefaultEndpointRegistrar.cs b/src/Dibix.Http.Host/Registration/DefaultEndpointRegistrar.cs index 3c948ae5..b8382183 100644 --- a/src/Dibix.Http.Host/Registration/DefaultEndpointRegistrar.cs +++ b/src/Dibix.Http.Host/Registration/DefaultEndpointRegistrar.cs @@ -26,11 +26,7 @@ public void Register(IEndpointRouteBuilder builder) { foreach (EndpointDefinition endpoint in _endpointMetadataProvider.GetEndpoints()) { - string baseAddress = ""; - if (!String.IsNullOrEmpty(_hostingOptions.Value.BaseAddress)) - baseAddress = $"/{_hostingOptions.Value.BaseAddress.Trim('/')}"; - - string route = $"{baseAddress}{endpoint.Url}"; + string route = $"{endpoint.Url}"; _logger.LogDebug("Registering route: {method} {route}", endpoint.Method, route); IEndpointConventionBuilder endpointBuilder = builder.MapMethods(route, EnumerableExtensions.Create(endpoint.Method), _endpointImplementationProvider.GetImplementation(endpoint));