Skip to content

Commit fd9c1a0

Browse files
Fixes issue with the IVirtualPageController not setting PublishedRequest content via FindContent (#15121)
* Fixes issue #12834 * Re-worked based on feedback * Use dependency injection * Only call new constructor --------- Co-authored-by: Nikolaj Geisle <[email protected]>
1 parent c4021e2 commit fd9c1a0

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/Umbraco.Web.Common/Routing/UmbracoVirtualPageRoute.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
using Microsoft.AspNetCore.Mvc.Filters;
66
using Microsoft.AspNetCore.Routing;
77
using Microsoft.Extensions.DependencyInjection;
8+
using Umbraco.Cms.Core.DependencyInjection;
89
using Umbraco.Cms.Core.Models.PublishedContent;
910
using Umbraco.Cms.Core.Routing;
11+
using Umbraco.Cms.Core.Web;
1012
using Umbraco.Cms.Web.Common.Controllers;
1113
using Umbraco.Cms.Web.Common.Extensions;
1214

@@ -21,6 +23,7 @@ public class UmbracoVirtualPageRoute : IUmbracoVirtualPageRoute
2123
private readonly LinkParser _linkParser;
2224
private readonly UriUtility _uriUtility;
2325
private readonly IPublishedRouter _publishedRouter;
26+
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
2427

2528
/// <summary>
2629
/// Constructor.
@@ -29,16 +32,29 @@ public class UmbracoVirtualPageRoute : IUmbracoVirtualPageRoute
2932
/// <param name="linkParser">The link parser.</param>
3033
/// <param name="uriUtility">The Uri utility.</param>
3134
/// <param name="publishedRouter">The published router.</param>
35+
/// <param name="umbracoContextAccessor">The umbraco context accessor.</param>
3236
public UmbracoVirtualPageRoute(
3337
EndpointDataSource endpointDataSource,
3438
LinkParser linkParser,
3539
UriUtility uriUtility,
36-
IPublishedRouter publishedRouter)
40+
IPublishedRouter publishedRouter,
41+
IUmbracoContextAccessor umbracoContextAccessor)
3742
{
3843
_endpointDataSource = endpointDataSource;
3944
_linkParser = linkParser;
4045
_uriUtility = uriUtility;
4146
_publishedRouter = publishedRouter;
47+
_umbracoContextAccessor = umbracoContextAccessor;
48+
}
49+
50+
[Obsolete("Please use constructor that takes an IUmbracoContextAccessor instead, scheduled for removal in v17")]
51+
public UmbracoVirtualPageRoute(
52+
EndpointDataSource endpointDataSource,
53+
LinkParser linkParser,
54+
UriUtility uriUtility,
55+
IPublishedRouter publishedRouter)
56+
: this(endpointDataSource, linkParser, uriUtility, publishedRouter, StaticServiceProvider.Instance.GetRequiredService<IUmbracoContextAccessor>())
57+
{
4258
}
4359

4460
/// <summary>
@@ -157,7 +173,8 @@ public async Task<IPublishedRequest> CreatePublishedRequest(HttpContext httpCont
157173
requestBuilder.SetPublishedContent(publishedContent);
158174
_publishedRouter.RouteDomain(requestBuilder);
159175

160-
return requestBuilder.Build();
176+
// Ensure the culture and domain is set correctly for the published request
177+
return await _publishedRouter.RouteRequestAsync(requestBuilder, new RouteRequestOptions(Core.Routing.RouteDirection.Inbound));
161178
}
162179

163180
/// <summary>
@@ -171,6 +188,12 @@ public async Task SetRouteValues(HttpContext httpContext, IPublishedContent publ
171188
{
172189
IPublishedRequest publishedRequest = await CreatePublishedRequest(httpContext, publishedContent);
173190

191+
// Ensure the published request is set to the UmbracoContext
192+
if (_umbracoContextAccessor.TryGetUmbracoContext(out IUmbracoContext? umbracoContext))
193+
{
194+
umbracoContext.PublishedRequest = publishedRequest;
195+
}
196+
174197
var umbracoRouteValues = new UmbracoRouteValues(
175198
publishedRequest,
176199
controllerActionDescriptor);

0 commit comments

Comments
 (0)