Skip to content

Commit 233fab1

Browse files
authored
Preserve the RequestUrlPrefix of tenants when running the ModularBackgroundService (#18623)
1 parent 337350f commit 233fab1

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

src/OrchardCore/OrchardCore.Abstractions/BackgroundTasks/HttpContextExtensions.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/OrchardCore/OrchardCore/Modules/ModularBackgroundService.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ await shellScope.UsingAsync(async scope =>
166166
try
167167
{
168168
// Use the base url, if defined, to override the 'Scheme', 'Host' and 'PathBase'.
169-
_httpContextAccessor.HttpContext.SetBaseUrl((await siteService.GetSiteSettingsAsync()).BaseUrl);
169+
SetBaseUrl(_httpContextAccessor.HttpContext, (await siteService.GetSiteSettingsAsync()).BaseUrl);
170170
}
171171
catch (Exception ex) when (!ex.IsFatal())
172172
{
@@ -430,4 +430,27 @@ private void CleanSchedulers(string tenant, IEnumerable<IBackgroundTask> tasks)
430430
}
431431
}
432432
}
433+
434+
private static void SetBaseUrl(HttpContext context, string baseUrl)
435+
{
436+
if (!Uri.TryCreate(baseUrl, UriKind.Absolute, out var uri))
437+
{
438+
return;
439+
}
440+
441+
context.Request.Scheme = uri.Scheme;
442+
context.Request.Host = new HostString(uri.Host, uri.Port);
443+
444+
// Set the PathBase only if it's not just "/". Otherwise, the original path base is kept, which may
445+
// contain the RequestUrlPrefix if any.
446+
if (uri.AbsolutePath != "/")
447+
{
448+
context.Request.PathBase = uri.AbsolutePath;
449+
}
450+
451+
if (!string.IsNullOrWhiteSpace(uri.Query))
452+
{
453+
context.Request.QueryString = new QueryString(uri.Query);
454+
}
455+
}
433456
}

0 commit comments

Comments
 (0)