Refactor getting Urls
This commit is contained in:
@@ -97,6 +97,10 @@ namespace Umbraco.Core.Models.PublishedContent
|
||||
/// Gets the url of the content item.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>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.</para>
|
||||
/// <para>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.</para>
|
||||
|
||||
@@ -5,6 +5,10 @@
|
||||
/// </summary>
|
||||
public enum UrlMode
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates that the url provider should do what it has been configured to do.
|
||||
/// </summary>
|
||||
Default = 0,
|
||||
|
||||
/// <summary>
|
||||
/// Indicates that the url provider should produce relative urls exclusively.
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -145,7 +145,7 @@ namespace Umbraco.Web
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region ToIndexedArray
|
||||
|
||||
public static IndexedArrayItem<TContent>[] ToIndexedArray<TContent>(this IEnumerable<TContent> source)
|
||||
@@ -192,14 +192,19 @@ namespace Umbraco.Web
|
||||
#region MediaUrl
|
||||
|
||||
/// <summary>
|
||||
/// Gets the url for the media.
|
||||
/// Gets the url for a media.
|
||||
/// </summary>
|
||||
/// <param name="content">The content.</param>
|
||||
/// <param name="propertyAlias">The property alias to resolve the url from.</param>
|
||||
/// <param name="culture">The variation language.</param>
|
||||
/// <returns>The url for the content.</returns>
|
||||
/// <remarks>Better use the <c>GetMediaUrl</c> method but that method is here to complement <c>MediaUrlAbsolute()</c>.</remarks>
|
||||
public static string MediaUrl(this IPublishedContent content, string propertyAlias, string culture = null)
|
||||
/// <param name="content">The content item.</param>
|
||||
/// <param name="culture">The culture (use current culture by default).</param>
|
||||
/// <param name="mode">The url mode (use site configuration by default).</param>
|
||||
/// <param name="propertyAlias">The alias of the property (use 'umbracoFile' by default).</param>
|
||||
/// <returns>The url for the media.</returns>
|
||||
/// <remarks>
|
||||
/// <para>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.</para>
|
||||
/// </remarks>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the absolute url for the media.
|
||||
/// </summary>
|
||||
/// <param name="content">The content.</param>
|
||||
/// <param name="propertyAlias">The property alias to resolve the url from.</param>
|
||||
/// <param name="mode">The url mode.</param>
|
||||
/// <param name="culture">The variation language.</param>
|
||||
/// <returns>The absolute url for the media.</returns>
|
||||
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
|
||||
|
||||
@@ -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.
|
||||
/// </summary>
|
||||
/// <param name="id">The published content identifier.</param>
|
||||
/// <param name="mode">The url mode.</param>
|
||||
/// <param name="culture">A culture.</param>
|
||||
/// <param name="current">The current absolute url.</param>
|
||||
/// <returns>The url for the published content.</returns>
|
||||
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);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the url of a published content.
|
||||
@@ -97,40 +95,9 @@ namespace Umbraco.Web.Routing
|
||||
/// <param name="culture">A culture.</param>
|
||||
/// <param name="current">The current absolute url.</param>
|
||||
/// <returns>The url for the published content.</returns>
|
||||
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);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the url of a published content.
|
||||
/// </summary>
|
||||
/// <param name="id">The published content identifier.</param>
|
||||
/// <param name="culture">A culture.</param>
|
||||
/// <param name="current">The current absolute url.</param>
|
||||
/// <returns>The url for the published content.</returns>
|
||||
public string GetUrl(int id, string culture = null, Uri current = null)
|
||||
=> GetUrl(GetDocument(id), Mode, culture, current);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the url of a published content.
|
||||
/// </summary>
|
||||
/// <param name="id">The published content identifier.</param>
|
||||
/// <param name="mode">The url mode.</param>
|
||||
/// <param name="culture">A culture.</param>
|
||||
/// <param name="current">The current absolute url.</param>
|
||||
/// <returns>The url for the published content.</returns>
|
||||
public string GetUrl(int id, UrlMode mode, string culture = null, Uri current = null)
|
||||
=> GetUrl(GetDocument(id), mode, culture, current);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the url of a published content.
|
||||
/// </summary>
|
||||
/// <param name="content">The published content.</param>
|
||||
/// <param name="culture">A culture.</param>
|
||||
/// <param name="current">The current absolute url.</param>
|
||||
/// <returns>The url for the published content.</returns>
|
||||
public string GetUrl(IPublishedContent content, string culture = null, Uri current = null)
|
||||
=> GetUrl(content, Mode, culture, current);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the url of a published content.
|
||||
/// </summary>
|
||||
@@ -145,11 +112,14 @@ namespace Umbraco.Web.Routing
|
||||
/// when no culture is specified, the current culture.</para>
|
||||
/// <para>If the provider is unable to provide a url, it returns "#".</para>
|
||||
/// </remarks>
|
||||
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
|
||||
|
||||
/// <summary>
|
||||
/// Gets the url of a media item.
|
||||
/// </summary>
|
||||
/// <param name="content">The published content.</param>
|
||||
/// <param name="propertyAlias">The property alias to resolve the url from.</param>
|
||||
/// <param name="culture">The variation language.</param>
|
||||
/// <param name="current">The current absolute url.</param>
|
||||
/// <returns>The url for the media.</returns>
|
||||
/// <remarks>
|
||||
/// <para>The url is absolute or relative depending on <c>mode</c> and on <c>current</c>.</para>
|
||||
/// <para>If the media is multi-lingual, gets the url for the specified culture or,
|
||||
/// when no culture is specified, the current culture.</para>
|
||||
/// <para>If the provider is unable to provide a url, it returns <see cref="String.Empty"/>.</para>
|
||||
/// </remarks>
|
||||
public string GetMediaUrl(IPublishedContent content, string propertyAlias, string culture = null, Uri current = null)
|
||||
=> GetMediaUrl(content, propertyAlias, Mode, culture, current);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the url of a media item.
|
||||
/// </summary>
|
||||
@@ -246,13 +199,16 @@ namespace Umbraco.Web.Routing
|
||||
/// when no culture is specified, the current culture.</para>
|
||||
/// <para>If the provider is unable to provide a url, it returns <see cref="String.Empty"/>.</para>
|
||||
/// </remarks>
|
||||
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)
|
||||
|
||||
@@ -198,7 +198,7 @@ namespace Umbraco.Web
|
||||
/// <returns>The url for the content.</returns>
|
||||
public string Url(int contentId, string culture = null)
|
||||
{
|
||||
return UrlProvider.GetUrl(contentId, culture);
|
||||
return UrlProvider.GetUrl(contentId, culture: culture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -209,7 +209,7 @@ namespace Umbraco.Web
|
||||
/// <returns>The url for the content.</returns>
|
||||
public string Url(Guid contentId, string culture = null)
|
||||
{
|
||||
return UrlProvider.GetUrl(contentId, culture);
|
||||
return UrlProvider.GetUrl(contentId, culture: culture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user