diff --git a/src/Umbraco.Core/Routing/AliasUrlProvider.cs b/src/Umbraco.Core/Routing/AliasUrlProvider.cs index ab0f940543..54d246219e 100644 --- a/src/Umbraco.Core/Routing/AliasUrlProvider.cs +++ b/src/Umbraco.Core/Routing/AliasUrlProvider.cs @@ -58,10 +58,7 @@ namespace Umbraco.Cms.Core.Routing /// public IEnumerable GetOtherUrls(int id, Uri current) { - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("A current Umbraco context is not available"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); var node = umbracoContext.Content.GetById(id); if (node == null) yield break; diff --git a/src/Umbraco.Core/Routing/ContentFinderByPageIdQuery.cs b/src/Umbraco.Core/Routing/ContentFinderByPageIdQuery.cs index f891e706c1..bcb0e461d3 100644 --- a/src/Umbraco.Core/Routing/ContentFinderByPageIdQuery.cs +++ b/src/Umbraco.Core/Routing/ContentFinderByPageIdQuery.cs @@ -1,7 +1,6 @@ - -using System; using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Core.Web; +using Umbraco.Extensions; namespace Umbraco.Cms.Core.Routing { @@ -29,10 +28,7 @@ namespace Umbraco.Cms.Core.Routing /// public bool TryFindContent(IPublishedRequestBuilder frequest) { - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); if (int.TryParse(_requestAccessor.GetRequestValue("umbPageID"), out int pageId)) { diff --git a/src/Umbraco.Core/Routing/ContentFinderByUrlAlias.cs b/src/Umbraco.Core/Routing/ContentFinderByUrlAlias.cs index 302d92211c..34b8ebe822 100644 --- a/src/Umbraco.Core/Routing/ContentFinderByUrlAlias.cs +++ b/src/Umbraco.Core/Routing/ContentFinderByUrlAlias.cs @@ -44,10 +44,7 @@ namespace Umbraco.Cms.Core.Routing /// A value indicating whether an Umbraco document was found and assigned. public bool TryFindContent(IPublishedRequestBuilder frequest) { - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); IPublishedContent node = null; diff --git a/src/Umbraco.Core/Routing/DefaultUrlProvider.cs b/src/Umbraco.Core/Routing/DefaultUrlProvider.cs index cb7f1f62d7..4e5d854250 100644 --- a/src/Umbraco.Core/Routing/DefaultUrlProvider.cs +++ b/src/Umbraco.Core/Routing/DefaultUrlProvider.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Options; using Umbraco.Cms.Core.Configuration.Models; using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Core.Web; +using Umbraco.Extensions; namespace Umbraco.Cms.Core.Routing { @@ -34,10 +35,7 @@ namespace Umbraco.Cms.Core.Routing public virtual UrlInfo GetUrl(IPublishedContent content, UrlMode mode, string culture, Uri current) { if (!current.IsAbsoluteUri) throw new ArgumentException("Current URL must be absolute.", nameof(current)); - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); // will not use cache if previewing var route = umbracoContext.Content.GetRouteById(content.Id, culture); @@ -83,10 +81,7 @@ namespace Umbraco.Cms.Core.Routing /// public virtual IEnumerable GetOtherUrls(int id, Uri current) { - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); var node = umbracoContext.Content.GetById(id); if (node == null) { diff --git a/src/Umbraco.Core/Routing/PublishedRouter.cs b/src/Umbraco.Core/Routing/PublishedRouter.cs index 746c4c9e7f..548387a675 100644 --- a/src/Umbraco.Core/Routing/PublishedRouter.cs +++ b/src/Umbraco.Core/Routing/PublishedRouter.cs @@ -265,10 +265,7 @@ namespace Umbraco.Cms.Core.Routing // note - we are not handling schemes nor ports here. _logger.LogDebug("{TracePrefix}Uri={RequestUri}", tracePrefix, request.Uri); - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); IDomainCache domainsCache = umbracoContext.PublishedSnapshot.Domains; var domains = domainsCache.GetAll(includeWildcards: false).ToList(); @@ -347,10 +344,7 @@ namespace Umbraco.Cms.Core.Routing var nodePath = request.PublishedContent.Path; _logger.LogDebug("{TracePrefix}Path={NodePath}", tracePrefix, nodePath); var rootNodeId = request.Domain != null ? request.Domain.ContentId : (int?)null; - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); Domain domain = DomainUtilities.FindWildcardDomainInPath(umbracoContext.PublishedSnapshot.Domains.GetAll(true), nodePath, rootNodeId); // always has a contentId and a culture @@ -504,10 +498,7 @@ namespace Umbraco.Cms.Core.Routing var valid = false; IPublishedContent internalRedirectNode = null; var internalRedirectId = request.PublishedContent.Value(_publishedValueFallback, Constants.Conventions.Content.InternalRedirectId, defaultValue: -1); - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); if (internalRedirectId > 0) { diff --git a/src/Umbraco.Core/Routing/UrlProvider.cs b/src/Umbraco.Core/Routing/UrlProvider.cs index 3ebfa7f80b..018343d02a 100644 --- a/src/Umbraco.Core/Routing/UrlProvider.cs +++ b/src/Umbraco.Core/Routing/UrlProvider.cs @@ -50,26 +50,17 @@ namespace Umbraco.Cms.Core.Routing private IPublishedContent GetDocument(int id) { - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("A current Umbraco context is not available"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); return umbracoContext.Content.GetById(id); } private IPublishedContent GetDocument(Guid id) { - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("A current Umbraco context is not available"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); return umbracoContext.Content.GetById(id); } private IPublishedContent GetMedia(Guid id) { - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("A current Umbraco context is not available"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); return umbracoContext.Media.GetById(id); } @@ -128,10 +119,7 @@ namespace Umbraco.Cms.Core.Routing if (current == null) { - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("A current Umbraco context is not available"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); current = umbracoContext.CleanedUmbracoUrl; } @@ -143,10 +131,7 @@ namespace Umbraco.Cms.Core.Routing public string GetUrlFromRoute(int id, string route, string culture) { - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("A current Umbraco context is not available"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); var provider = _urlProviders.OfType().FirstOrDefault(); var url = provider == null ? route // what else? @@ -170,10 +155,7 @@ namespace Umbraco.Cms.Core.Routing /// public IEnumerable GetOtherUrls(int id) { - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("A current Umbraco context is not available"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); return GetOtherUrls(id, umbracoContext.CleanedUmbracoUrl); } @@ -245,10 +227,7 @@ namespace Umbraco.Cms.Core.Routing if (current == null) { - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("A current Umbraco context is not available"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); current = umbracoContext.CleanedUmbracoUrl; } diff --git a/src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs b/src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs index bdb221de42..2cc5a918df 100644 --- a/src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs +++ b/src/Umbraco.Core/Templates/HtmlLocalLinkParser.cs @@ -41,10 +41,7 @@ namespace Umbraco.Cms.Core.Templates /// public string EnsureInternalLinks(string text, bool preview) { - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("Could not parse internal links, there is no current UmbracoContext"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); if (!preview) return EnsureInternalLinks(text); diff --git a/src/Umbraco.Core/Templates/UmbracoComponentRenderer.cs b/src/Umbraco.Core/Templates/UmbracoComponentRenderer.cs index d2aad81163..c43a6f6929 100644 --- a/src/Umbraco.Core/Templates/UmbracoComponentRenderer.cs +++ b/src/Umbraco.Core/Templates/UmbracoComponentRenderer.cs @@ -66,10 +66,7 @@ namespace Umbraco.Cms.Core.Templates { throw new ArgumentException("Invalid content id " + contentId); } - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("A current Umbraco context is not available"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); var content = umbracoContext.Content?.GetById(contentId); if (content == null) diff --git a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs index 98ec92ec70..2ddd636cd2 100644 --- a/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs +++ b/src/Umbraco.Infrastructure/DependencyInjection/UmbracoBuilder.CoreServices.cs @@ -166,10 +166,7 @@ namespace Umbraco.Cms.Infrastructure.DependencyInjection builder.Services.AddScoped(factory => { var umbCtx = factory.GetRequiredService(); - if (!umbCtx.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); - } + var umbracoContext = umbCtx.GetRequiredUmbracoContext(); return new PublishedContentQuery(umbracoContext.PublishedSnapshot, factory.GetRequiredService(), factory.GetRequiredService()); }); diff --git a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs index 2f01388edb..82b1305fe4 100644 --- a/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs +++ b/src/Umbraco.Infrastructure/PropertyEditors/ValueConverters/RteMacroRenderingValueConverter.cs @@ -51,10 +51,7 @@ namespace Umbraco.Cms.Core.PropertyEditors.ValueConverters // different preview modes. private string RenderRteMacros(string source, bool preview) { - if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext)) - { - throw new InvalidOperationException("Wasn't able to get an UmbracoContext"); - } + var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext(); using (umbracoContext.ForcedPreview(preview)) // force for macro rendering { var sb = new StringBuilder();