@@ -60,7 +60,7 @@ private static void ValidateDatabaseVersionCompatibility(Smtp4devDbContext conte
6060
6161 // Get all migrations that have been applied to the database
6262 var appliedMigrations = context . Database . GetAppliedMigrations ( ) . ToList ( ) ;
63-
63+
6464 // Get all migrations available in the current application
6565 var availableMigrations = context . Database . GetMigrations ( ) . ToList ( ) ;
6666
@@ -92,7 +92,7 @@ public void ConfigureServices(IServiceCollection services)
9292 //Remove the JSON content type from the actions where it is not supported.
9393 NSwag . OpenApiOperationDescription sendOp = d . Operations . FirstOrDefault ( o => o . Path . EndsWith ( "/send" ) || o . Path . EndsWith ( "/reply" ) ) ;
9494 sendOp . Operation . RequestBody . Content . Remove ( "application/json" ) ;
95-
95+
9696 } ;
9797 } ) ;
9898
@@ -131,10 +131,10 @@ public void ConfigureServices(IServiceCollection services)
131131
132132
133133 using var context = new Smtp4devDbContext ( ( DbContextOptions < Smtp4devDbContext > ) opt . Options ) ;
134-
134+
135135 // Validate database version compatibility before attempting any operations
136136 ValidateDatabaseVersionCompatibility ( context ) ;
137-
137+
138138 if ( string . IsNullOrEmpty ( serverOptions . Database ) )
139139 {
140140 context . Database . Migrate ( ) ;
@@ -178,7 +178,7 @@ public void ConfigureServices(IServiceCollection services)
178178 {
179179 Log . Logger . Information ( "Populating MIME metadata for {count} existing messages during startup" , messagesWithoutMetadata . Count ) ;
180180 var mimeProcessingService = new MimeProcessingService ( ) ;
181-
181+
182182 int processed = 0 ;
183183 int batchSize = 50 ; // Process in batches to avoid memory issues
184184
@@ -335,14 +335,30 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
335335
336336 if ( ! string . IsNullOrEmpty ( serverOptions . BasePath ) && serverOptions . BasePath != "/" )
337337 {
338- RewriteOptions rewrites = new RewriteOptions ( ) ;
339- rewrites . AddRedirect ( "^" + serverOptions . BasePath . TrimEnd ( '/' ) + "$" , serverOptions . BasePath . TrimEnd ( '/' ) + "/" ) ;
340- ;
341- rewrites . AddRedirect ( "^(/)?$" , serverOptions . BasePath . TrimEnd ( '/' ) + "/" ) ;
342- ;
343- app . UseRewriter ( rewrites ) ;
344-
345- app . Map ( serverOptions . BasePath , configure ) ;
338+ string basePathNoSlash = serverOptions . BasePath . TrimEnd ( '/' ) ;
339+ string redirectTarget = basePathNoSlash + "/" ;
340+
341+ // Global middleware to redirect /smtp4dev to /smtp4dev/
342+ app . Use ( async ( context , next ) =>
343+ {
344+ if ( context . Request . Path . Equals ( basePathNoSlash , StringComparison . OrdinalIgnoreCase )
345+ && ! context . Request . Path . Value . EndsWith ( "/" ) )
346+ {
347+ var queryString = context . Request . QueryString . HasValue ? context . Request . QueryString . Value : "" ;
348+ context . Response . Redirect ( redirectTarget + queryString , true ) ;
349+ return ;
350+ }
351+ else if ( context . Request . Path . Value . Equals ( "/" )
352+ || context . Request . Path . Value == String . Empty )
353+ {
354+ var queryString = context . Request . QueryString . HasValue ? context . Request . QueryString . Value : "" ;
355+ context . Response . Redirect ( redirectTarget + queryString , true ) ;
356+ return ;
357+ }
358+ await next ( ) ;
359+ } ) ;
360+
361+ app . Map ( serverOptions . BasePath . TrimEnd ( '/' ) , configure ) ;
346362 }
347363 else
348364 {
0 commit comments