5
5
using Microsoft . AspNetCore . Mvc . Filters ;
6
6
using Microsoft . AspNetCore . Routing ;
7
7
using Microsoft . Extensions . DependencyInjection ;
8
+ using Umbraco . Cms . Core . DependencyInjection ;
8
9
using Umbraco . Cms . Core . Models . PublishedContent ;
9
10
using Umbraco . Cms . Core . Routing ;
11
+ using Umbraco . Cms . Core . Web ;
10
12
using Umbraco . Cms . Web . Common . Controllers ;
11
13
using Umbraco . Cms . Web . Common . Extensions ;
12
14
@@ -21,6 +23,7 @@ public class UmbracoVirtualPageRoute : IUmbracoVirtualPageRoute
21
23
private readonly LinkParser _linkParser ;
22
24
private readonly UriUtility _uriUtility ;
23
25
private readonly IPublishedRouter _publishedRouter ;
26
+ private readonly IUmbracoContextAccessor _umbracoContextAccessor ;
24
27
25
28
/// <summary>
26
29
/// Constructor.
@@ -29,16 +32,29 @@ public class UmbracoVirtualPageRoute : IUmbracoVirtualPageRoute
29
32
/// <param name="linkParser">The link parser.</param>
30
33
/// <param name="uriUtility">The Uri utility.</param>
31
34
/// <param name="publishedRouter">The published router.</param>
35
+ /// <param name="umbracoContextAccessor">The umbraco context accessor.</param>
32
36
public UmbracoVirtualPageRoute (
33
37
EndpointDataSource endpointDataSource ,
34
38
LinkParser linkParser ,
35
39
UriUtility uriUtility ,
36
- IPublishedRouter publishedRouter )
40
+ IPublishedRouter publishedRouter ,
41
+ IUmbracoContextAccessor umbracoContextAccessor )
37
42
{
38
43
_endpointDataSource = endpointDataSource ;
39
44
_linkParser = linkParser ;
40
45
_uriUtility = uriUtility ;
41
46
_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
+ {
42
58
}
43
59
44
60
/// <summary>
@@ -157,7 +173,8 @@ public async Task<IPublishedRequest> CreatePublishedRequest(HttpContext httpCont
157
173
requestBuilder . SetPublishedContent ( publishedContent ) ;
158
174
_publishedRouter . RouteDomain ( requestBuilder ) ;
159
175
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 ) ) ;
161
178
}
162
179
163
180
/// <summary>
@@ -171,6 +188,12 @@ public async Task SetRouteValues(HttpContext httpContext, IPublishedContent publ
171
188
{
172
189
IPublishedRequest publishedRequest = await CreatePublishedRequest ( httpContext , publishedContent ) ;
173
190
191
+ // Ensure the published request is set to the UmbracoContext
192
+ if ( _umbracoContextAccessor . TryGetUmbracoContext ( out IUmbracoContext ? umbracoContext ) )
193
+ {
194
+ umbracoContext . PublishedRequest = publishedRequest ;
195
+ }
196
+
174
197
var umbracoRouteValues = new UmbracoRouteValues (
175
198
publishedRequest ,
176
199
controllerActionDescriptor ) ;
0 commit comments