Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mac McDell 73 posts 148 karma points
    Jun 10, 2016 @ 18:46
    Mac McDell
    0

    UrlAbsolute and UrlWIthDomain not supported for PublishedItemType.Media ?

    Hi. Trying to get url with domain for reverse proxy reasons. I need to get all my media, content urls etc to return the current domains. I am trying to use use UrlWithDomain() which I see just returns UrlAbsolute() in the source. However this is the method:

     public static string UrlAbsolute(this IPublishedContent content)
        {
          switch (content.ItemType)
          {
            case PublishedItemType.Content:
              if (UmbracoContext.Current == null)
                throw new InvalidOperationException("Cannot resolve a Url for a content item when UmbracoContext.Current is null.");
              if (UmbracoContext.Current.UrlProvider == null)
                throw new InvalidOperationException("Cannot resolve a Url for a content item when UmbracoContext.Current.UrlProvider is null.");
              return UmbracoContext.Current.UrlProvider.GetUrl(content.Id, true);
            case PublishedItemType.Media:
              throw new NotSupportedException("AbsoluteUrl is not supported for media types.");
            default:
              throw new ArgumentOutOfRangeException();
          }
        }
    

    So the question is... if media is not supported using UrlAbsolute() is there a alternate static that is without writing our own extension?

  • Mac McDell 73 posts 148 karma points
    Jun 13, 2016 @ 16:25
    Mac McDell
    1

    if anyone else runs into this issue I just ended up writing a custom string extension that essentially got me what I needed.

     public static class StringExtensions
    {
        /// <summary>Gets the absolute url for the media.</summary>
        /// <param name="UrlPath">The content.</param>
        /// <returns>The absolute url for the media.</returns>
        public static string WithDomain(this string UrlPath)
        {
           Uri domain =  HttpContext.Current.Request.Url;
    
            if (UrlPath.IndexOf('/') != 0)
                UrlPath = "/" + UrlPath; 
    
            return domain.GetLeftPart(UriPartial.Authority) +  UrlPath;
        }   
      }
    
Please Sign in or register to post replies

Write your reply to:

Draft