15
15
namespace Umbraco . Cms . Core . Routing ;
16
16
17
17
/// <summary>
18
- /// Provides urls .
18
+ /// Provides URLs .
19
19
/// </summary>
20
20
public class NewDefaultUrlProvider : IUrlProvider
21
21
{
22
- private readonly ILocalizationService _localizationService ;
23
22
private readonly IPublishedContentCache _publishedContentCache ;
24
23
private readonly IDomainCache _domainCache ;
25
24
private readonly IIdKeyMap _idKeyMap ;
26
25
private readonly IDocumentUrlService _documentUrlService ;
27
26
private readonly IDocumentNavigationQueryService _navigationQueryService ;
28
27
private readonly IPublishedContentStatusFilteringService _publishedContentStatusFilteringService ;
29
- private readonly ILocalizedTextService ? _localizedTextService ;
30
28
private readonly ILogger < DefaultUrlProvider > _logger ;
31
29
private readonly ISiteDomainMapper _siteDomainMapper ;
32
30
private readonly IUmbracoContextAccessor _umbracoContextAccessor ;
33
31
private readonly UriUtility _uriUtility ;
34
32
private RequestHandlerSettings _requestSettings ;
33
+ private readonly ILanguageService _languageService ;
35
34
35
+ // TODO (V17): When removing the obsolete constructors, remove the unused localizationService parameter from the constructor (we can't do it now
36
+ // because it is used in the obsolete constructors and leads to an ambigious constructor error).
37
+ // See also if we can make GetUrlFromRoute asynchronous and avoid the GetAwaiter().GetResult() in when using ILanguageService.
38
+
39
+ /// <summary>
40
+ /// Initializes a new instance of the <see cref="NewDefaultUrlProvider"/> class.
41
+ /// </summary>
36
42
public NewDefaultUrlProvider (
37
43
IOptionsMonitor < RequestHandlerSettings > requestSettings ,
38
44
ILogger < DefaultUrlProvider > logger ,
39
45
ISiteDomainMapper siteDomainMapper ,
40
46
IUmbracoContextAccessor umbracoContextAccessor ,
41
47
UriUtility uriUtility ,
48
+ #pragma warning disable CS0618 // Type or member is obsolete
49
+ #pragma warning disable IDE0060 // Remove unused parameter
42
50
ILocalizationService localizationService ,
51
+ #pragma warning restore IDE0060 // Remove unused parameter
52
+ #pragma warning restore CS0618 // Type or member is obsolete
43
53
IPublishedContentCache publishedContentCache ,
44
54
IDomainCache domainCache ,
45
55
IIdKeyMap idKeyMap ,
46
56
IDocumentUrlService documentUrlService ,
47
57
IDocumentNavigationQueryService navigationQueryService ,
48
- IPublishedContentStatusFilteringService publishedContentStatusFilteringService )
58
+ IPublishedContentStatusFilteringService publishedContentStatusFilteringService ,
59
+ ILanguageService languageService )
49
60
{
50
61
_requestSettings = requestSettings . CurrentValue ;
51
62
_logger = logger ;
52
63
_siteDomainMapper = siteDomainMapper ;
53
64
_umbracoContextAccessor = umbracoContextAccessor ;
54
65
_uriUtility = uriUtility ;
55
- _localizationService = localizationService ;
56
66
_publishedContentCache = publishedContentCache ;
57
67
_domainCache = domainCache ;
58
68
_idKeyMap = idKeyMap ;
59
69
_documentUrlService = documentUrlService ;
60
70
_navigationQueryService = navigationQueryService ;
61
71
_publishedContentStatusFilteringService = publishedContentStatusFilteringService ;
72
+ _languageService = languageService ;
62
73
63
74
requestSettings . OnChange ( x => _requestSettings = x ) ;
64
75
}
65
76
77
+ /// <summary>
78
+ /// Initializes a new instance of the <see cref="NewDefaultUrlProvider"/> class.
79
+ /// </summary>
80
+ [ Obsolete ( "Use the non-obsolete constructor. Scheduled for removal in V17." ) ]
81
+ public NewDefaultUrlProvider (
82
+ IOptionsMonitor < RequestHandlerSettings > requestSettings ,
83
+ ILogger < DefaultUrlProvider > logger ,
84
+ ISiteDomainMapper siteDomainMapper ,
85
+ IUmbracoContextAccessor umbracoContextAccessor ,
86
+ UriUtility uriUtility ,
87
+ ILocalizationService localizationService ,
88
+ IPublishedContentCache publishedContentCache ,
89
+ IDomainCache domainCache ,
90
+ IIdKeyMap idKeyMap ,
91
+ IDocumentUrlService documentUrlService ,
92
+ IDocumentNavigationQueryService navigationQueryService ,
93
+ IPublishedContentStatusFilteringService publishedContentStatusFilteringService )
94
+ : this (
95
+ requestSettings ,
96
+ logger ,
97
+ siteDomainMapper ,
98
+ umbracoContextAccessor ,
99
+ uriUtility ,
100
+ localizationService ,
101
+ publishedContentCache ,
102
+ domainCache ,
103
+ idKeyMap ,
104
+ documentUrlService ,
105
+ navigationQueryService ,
106
+ publishedContentStatusFilteringService ,
107
+ StaticServiceProvider . Instance . GetRequiredService < ILanguageService > ( ) )
108
+ {
109
+ }
110
+
111
+ /// <summary>
112
+ /// Initializes a new instance of the <see cref="NewDefaultUrlProvider"/> class.
113
+ /// </summary>
66
114
[ Obsolete ( "Use the non-obsolete constructor. Scheduled for removal in V17." ) ]
67
115
public NewDefaultUrlProvider (
68
116
IOptionsMonitor < RequestHandlerSettings > requestSettings ,
@@ -92,8 +140,6 @@ public NewDefaultUrlProvider(
92
140
{
93
141
}
94
142
95
- #region GetOtherUrls
96
-
97
143
/// <summary>
98
144
/// Gets the other URLs of a published content.
99
145
/// </summary>
@@ -108,14 +154,14 @@ public NewDefaultUrlProvider(
108
154
/// </remarks>
109
155
public virtual IEnumerable < UrlInfo > GetOtherUrls ( int id , Uri current )
110
156
{
111
- var keyAttempt = _idKeyMap . GetKeyForId ( id , UmbracoObjectTypes . Document ) ;
157
+ Attempt < Guid > keyAttempt = _idKeyMap . GetKeyForId ( id , UmbracoObjectTypes . Document ) ;
112
158
113
159
if ( keyAttempt . Success is false )
114
160
{
115
161
yield break ;
116
162
}
117
163
118
- var key = keyAttempt . Result ;
164
+ Guid key = keyAttempt . Result ;
119
165
120
166
IPublishedContent ? node = _publishedContentCache . GetById ( key ) ;
121
167
if ( node == null )
@@ -156,7 +202,7 @@ public virtual IEnumerable<UrlInfo> GetOtherUrls(int id, Uri current)
156
202
157
203
// need to strip off the leading ID for the route if it exists (occurs if the route is for a node with a domain assigned)
158
204
var pos = route . IndexOf ( '/' , StringComparison . Ordinal ) ;
159
- var path = pos == 0 ? route : route . Substring ( pos ) ;
205
+ var path = pos == 0 ? route : route [ pos .. ] ;
160
206
161
207
var uri = new Uri ( CombinePaths ( d . Uri . GetLeftPart ( UriPartial . Path ) , path ) ) ;
162
208
uri = _uriUtility . UriFromUmbraco ( uri , _requestSettings ) ;
@@ -178,17 +224,9 @@ public virtual IEnumerable<UrlInfo> GetOtherUrls(int id, Uri current)
178
224
private string GetLegacyRouteFormatById ( Guid key , string ? culture )
179
225
{
180
226
var isDraft = _umbracoContextAccessor . GetRequiredUmbracoContext ( ) . InPreviewMode ;
181
-
182
-
183
227
return _documentUrlService . GetLegacyRouteFormat ( key , culture , isDraft ) ;
184
-
185
-
186
228
}
187
229
188
- #endregion
189
-
190
- #region GetUrl
191
-
192
230
/// <inheritdoc />
193
231
public virtual UrlInfo ? GetUrl ( IPublishedContent content , UrlMode mode , string ? culture , Uri current )
194
232
{
@@ -217,6 +255,9 @@ private string GetLegacyRouteFormatById(Guid key, string? culture)
217
255
return GetUrlFromRoute ( route , content . Id , current , mode , culture ) ;
218
256
}
219
257
258
+ /// <summary>
259
+ /// Gets the URL from the provided route.
260
+ /// </summary>
220
261
internal UrlInfo ? GetUrlFromRoute (
221
262
string ? route ,
222
263
int id ,
@@ -226,7 +267,7 @@ private string GetLegacyRouteFormatById(Guid key, string? culture)
226
267
{
227
268
if ( string . IsNullOrWhiteSpace ( route ) || route . Equals ( "#" ) )
228
269
{
229
- if ( _logger . IsEnabled ( Microsoft . Extensions . Logging . LogLevel . Debug ) )
270
+ if ( _logger . IsEnabled ( LogLevel . Debug ) )
230
271
{
231
272
_logger . LogDebug (
232
273
"Couldn't find any page with nodeId={NodeId}. This is most likely caused by the page not being published." ,
@@ -248,7 +289,7 @@ private string GetLegacyRouteFormatById(Guid key, string? culture)
248
289
current ,
249
290
culture ) ;
250
291
251
- var defaultCulture = _localizationService . GetDefaultLanguageIsoCode ( ) ;
292
+ var defaultCulture = _languageService . GetDefaultIsoCodeAsync ( ) . GetAwaiter ( ) . GetResult ( ) ;
252
293
if ( domainUri is not null ||
253
294
string . IsNullOrEmpty ( culture ) ||
254
295
culture . Equals ( defaultCulture , StringComparison . InvariantCultureIgnoreCase ) )
@@ -260,10 +301,6 @@ private string GetLegacyRouteFormatById(Guid key, string? culture)
260
301
return null ;
261
302
}
262
303
263
- #endregion
264
-
265
- #region Utilities
266
-
267
304
private Uri AssembleUrl ( DomainAndUri ? domainUri , string path , Uri current , UrlMode mode )
268
305
{
269
306
Uri uri ;
@@ -331,6 +368,4 @@ private string CombinePaths(string path1, string path2)
331
368
var path = path1 . TrimEnd ( Constants . CharArrays . ForwardSlash ) + path2 ;
332
369
return path == "/" ? path : path . TrimEnd ( Constants . CharArrays . ForwardSlash ) ;
333
370
}
334
-
335
- #endregion
336
371
}
0 commit comments