From 25b8c8a5652d396f98e525a17fbe11d7926ba4d6 Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 24 Apr 2019 11:53:35 +0200 Subject: [PATCH] Refactor getting Urls --- .../PublishedContent/IPublishedContent.cs | 4 ++ .../Models/PublishedContent/UrlMode.cs | 4 ++ .../Models/PublishedContentBase.cs | 2 +- src/Umbraco.Web/PublishedElementExtensions.cs | 43 ++++-------- src/Umbraco.Web/Routing/UrlProvider.cs | 68 ++++--------------- src/Umbraco.Web/UmbracoContext.cs | 4 +- 6 files changed, 37 insertions(+), 88 deletions(-) diff --git a/src/Umbraco.Core/Models/PublishedContent/IPublishedContent.cs b/src/Umbraco.Core/Models/PublishedContent/IPublishedContent.cs index 8ff648553c..6917538331 100644 --- a/src/Umbraco.Core/Models/PublishedContent/IPublishedContent.cs +++ b/src/Umbraco.Core/Models/PublishedContent/IPublishedContent.cs @@ -97,6 +97,10 @@ namespace Umbraco.Core.Models.PublishedContent /// Gets the url of the content item. /// /// + /// If the content item is a document, then this method returns the url of the + /// document. If it is a media, then this methods return the media url for the + /// 'umbracoFile' property. Use the MediaUrl() method to get the media url for other + /// properties. /// The value of this property is contextual. It depends on the 'current' request uri, /// if any. In addition, when the content type is multi-lingual, this is the url for the /// specified culture. Otherwise, it is the invariant url. diff --git a/src/Umbraco.Core/Models/PublishedContent/UrlMode.cs b/src/Umbraco.Core/Models/PublishedContent/UrlMode.cs index f19f93bec1..4cd6a680f4 100644 --- a/src/Umbraco.Core/Models/PublishedContent/UrlMode.cs +++ b/src/Umbraco.Core/Models/PublishedContent/UrlMode.cs @@ -5,6 +5,10 @@ /// public enum UrlMode { + /// + /// Indicates that the url provider should do what it has been configured to do. + /// + Default = 0, /// /// Indicates that the url provider should produce relative urls exclusively. diff --git a/src/Umbraco.Web/Models/PublishedContentBase.cs b/src/Umbraco.Web/Models/PublishedContentBase.cs index 4fc08f6ff3..25c84c97e5 100644 --- a/src/Umbraco.Web/Models/PublishedContentBase.cs +++ b/src/Umbraco.Web/Models/PublishedContentBase.cs @@ -95,7 +95,7 @@ namespace Umbraco.Web.Models return umbracoContext.UrlProvider.GetUrl(this, mode, culture); case PublishedItemType.Media: - return umbracoContext.UrlProvider.GetMediaUrl(this, Constants.Conventions.Media.File, mode, culture); + return umbracoContext.UrlProvider.GetMediaUrl(this, mode, culture, Constants.Conventions.Media.File); default: throw new NotSupportedException(); diff --git a/src/Umbraco.Web/PublishedElementExtensions.cs b/src/Umbraco.Web/PublishedElementExtensions.cs index 49df6b2685..2de8259d3c 100644 --- a/src/Umbraco.Web/PublishedElementExtensions.cs +++ b/src/Umbraco.Web/PublishedElementExtensions.cs @@ -145,7 +145,7 @@ namespace Umbraco.Web } #endregion - + #region ToIndexedArray public static IndexedArrayItem[] ToIndexedArray(this IEnumerable source) @@ -192,14 +192,19 @@ namespace Umbraco.Web #region MediaUrl /// - /// Gets the url for the media. + /// Gets the url for a media. /// - /// The content. - /// The property alias to resolve the url from. - /// The variation language. - /// The url for the content. - /// Better use the GetMediaUrl method but that method is here to complement MediaUrlAbsolute(). - public static string MediaUrl(this IPublishedContent content, string propertyAlias, string culture = null) + /// The content item. + /// The culture (use current culture by default). + /// The url mode (use site configuration by default). + /// The alias of the property (use 'umbracoFile' by default). + /// The url for the media. + /// + /// The value of this property is contextual. It depends on the 'current' request uri, + /// if any. In addition, when the content type is multi-lingual, this is the url for the + /// specified culture. Otherwise, it is the invariant url. + /// + public static string MediaUrl(this IPublishedContent content, string culture = null, UrlMode mode = UrlMode.Default, string propertyAlias = Constants.Conventions.Media.File) { var umbracoContext = Composing.Current.UmbracoContext; @@ -208,27 +213,7 @@ namespace Umbraco.Web if (umbracoContext.UrlProvider == null) throw new InvalidOperationException("Cannot resolve a Url for a content item when Current.UmbracoContext.UrlProvider is null."); - return umbracoContext.UrlProvider.GetMediaUrl(content, propertyAlias, culture); - } - - /// - /// Gets the absolute url for the media. - /// - /// The content. - /// The property alias to resolve the url from. - /// The url mode. - /// The variation language. - /// The absolute url for the media. - public static string MediaUrl(this IPublishedContent content, string propertyAlias, UrlMode mode, string culture = null) - { - var umbracoContext = Composing.Current.UmbracoContext; - - if (umbracoContext == null) - throw new InvalidOperationException("Cannot resolve a Url for a content item when Current.UmbracoContext is null."); - if (umbracoContext.UrlProvider == null) - throw new InvalidOperationException("Cannot resolve a Url for a content item when Current.UmbracoContext.UrlProvider is null."); - - return umbracoContext.UrlProvider.GetMediaUrl(content, propertyAlias, mode, culture); + return umbracoContext.UrlProvider.GetMediaUrl(content, mode, culture, propertyAlias); } #endregion diff --git a/src/Umbraco.Web/Routing/UrlProvider.cs b/src/Umbraco.Web/Routing/UrlProvider.cs index 0f79d11364..59e39fa80a 100644 --- a/src/Umbraco.Web/Routing/UrlProvider.cs +++ b/src/Umbraco.Web/Routing/UrlProvider.cs @@ -3,11 +3,8 @@ using System.Collections.Generic; using System.Linq; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core; -using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; -using Umbraco.Core.Services; using Umbraco.Web.Composing; -using Umbraco.Web.Models; namespace Umbraco.Web.Routing { @@ -83,11 +80,12 @@ namespace Umbraco.Web.Routing /// Gets the url of a published content. /// /// The published content identifier. + /// The url mode. /// A culture. /// The current absolute url. /// The url for the published content. - public string GetUrl(Guid id, string culture = null, Uri current = null) - => GetUrl(GetDocument(id), Mode, culture, current); + public string GetUrl(Guid id, UrlMode mode = UrlMode.Default, string culture = null, Uri current = null) + => GetUrl(GetDocument(id), mode, culture, current); /// /// Gets the url of a published content. @@ -97,40 +95,9 @@ namespace Umbraco.Web.Routing /// A culture. /// The current absolute url. /// The url for the published content. - public string GetUrl(Guid id, UrlMode mode, string culture = null, Uri current = null) + public string GetUrl(int id, UrlMode mode = UrlMode.Default, string culture = null, Uri current = null) => GetUrl(GetDocument(id), mode, culture, current); - /// - /// Gets the url of a published content. - /// - /// The published content identifier. - /// A culture. - /// The current absolute url. - /// The url for the published content. - public string GetUrl(int id, string culture = null, Uri current = null) - => GetUrl(GetDocument(id), Mode, culture, current); - - /// - /// Gets the url of a published content. - /// - /// The published content identifier. - /// The url mode. - /// A culture. - /// The current absolute url. - /// The url for the published content. - public string GetUrl(int id, UrlMode mode, string culture = null, Uri current = null) - => GetUrl(GetDocument(id), mode, culture, current); - - /// - /// Gets the url of a published content. - /// - /// The published content. - /// A culture. - /// The current absolute url. - /// The url for the published content. - public string GetUrl(IPublishedContent content, string culture = null, Uri current = null) - => GetUrl(content, Mode, culture, current); - /// /// Gets the url of a published content. /// @@ -145,11 +112,14 @@ namespace Umbraco.Web.Routing /// when no culture is specified, the current culture. /// If the provider is unable to provide a url, it returns "#". /// - public string GetUrl(IPublishedContent content, UrlMode mode, string culture = null, Uri current = null) + public string GetUrl(IPublishedContent content, UrlMode mode = UrlMode.Default, string culture = null, Uri current = null) { if (content == null || content.ContentType.ItemType == PublishedItemType.Element) return "#"; + if (mode == UrlMode.Default) + mode = Mode; + // this the ONLY place where we deal with default culture - IUrlProvider always receive a culture // be nice with tests, assume things can be null, ultimately fall back to invariant // (but only for variant content of course) @@ -214,23 +184,6 @@ namespace Umbraco.Web.Routing #region GetMediaUrl - /// - /// Gets the url of a media item. - /// - /// The published content. - /// The property alias to resolve the url from. - /// The variation language. - /// The current absolute url. - /// The url for the media. - /// - /// The url is absolute or relative depending on mode and on current. - /// If the media is multi-lingual, gets the url for the specified culture or, - /// when no culture is specified, the current culture. - /// If the provider is unable to provide a url, it returns . - /// - public string GetMediaUrl(IPublishedContent content, string propertyAlias, string culture = null, Uri current = null) - => GetMediaUrl(content, propertyAlias, Mode, culture, current); - /// /// Gets the url of a media item. /// @@ -246,13 +199,16 @@ namespace Umbraco.Web.Routing /// when no culture is specified, the current culture. /// If the provider is unable to provide a url, it returns . /// - public string GetMediaUrl(IPublishedContent content, string propertyAlias, UrlMode mode, string culture = null, Uri current = null) + public string GetMediaUrl(IPublishedContent content, UrlMode mode = UrlMode.Default, string culture = null, string propertyAlias = Constants.Conventions.Media.File, Uri current = null) { if (propertyAlias == null) throw new ArgumentNullException(nameof(propertyAlias)); if (content == null) return ""; + if (mode == UrlMode.Default) + mode = Mode; + // this the ONLY place where we deal with default culture - IMediaUrlProvider always receive a culture // be nice with tests, assume things can be null, ultimately fall back to invariant // (but only for variant content of course) diff --git a/src/Umbraco.Web/UmbracoContext.cs b/src/Umbraco.Web/UmbracoContext.cs index 3947adc0e5..664818bda6 100644 --- a/src/Umbraco.Web/UmbracoContext.cs +++ b/src/Umbraco.Web/UmbracoContext.cs @@ -198,7 +198,7 @@ namespace Umbraco.Web /// The url for the content. public string Url(int contentId, string culture = null) { - return UrlProvider.GetUrl(contentId, culture); + return UrlProvider.GetUrl(contentId, culture: culture); } /// @@ -209,7 +209,7 @@ namespace Umbraco.Web /// The url for the content. public string Url(Guid contentId, string culture = null) { - return UrlProvider.GetUrl(contentId, culture); + return UrlProvider.GetUrl(contentId, culture: culture); } ///