Copied to clipboard

Flag this post as spam?

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


  • Christian John Schmidt 6 posts 106 karma points
    Apr 08, 2019 @ 11:49
    Christian John Schmidt
    0

    Url with domain in Umbraco 8

    In order to get an url with domain in V7 you could do like this:

                   UmbracoHelper uh = new UmbracoHelper(UmbracoContext.Current);
                    IPublishedContent content = uh.TypedContent(node.Id);
    
                    string url =content.UrlWithDomain()
    

    However the .UrlWithDomain() method has been removed in V8. Is there a similar way to do this? The code needs to work in an IComponent

  • Corné Strijkert 62 posts 405 karma points
    Apr 08, 2019 @ 17:45
    Corné Strijkert
    3

    Maybe you are searching for this method?

    @Umbraco.Content(id).UrlAbsolute()
    
  • Marc Goodson 1008 posts 6644 karma points MVP 4x c-trib
    Apr 08, 2019 @ 18:43
    Marc Goodson
    100

    Hi Christian

    Corné is correct UrlAbsolute() is the new name for UrlWithDomain()...

    inside a Component, you'll need to inject IUmbracoContextFactory, and use EnsureUmbracoContext() to be able to pull back the IPublishedContent item you are after...

    ... something like this:

    using System;
    using System.Collections.Generic;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Web;
    
    namespace Umbraco8.Components
    {
        public class ExampleComposer : ComponentComposer<ExampleComponent>
        {
    
        }
        public class ExampleComponent : IComponent
        {
            private readonly IUmbracoContextFactory _umbracoContextFactory;
    
    public ExampleComponent(IUmbracoContextFactory umbracoContextFactory)
            {
                _umbracoContextFactory = umbracoContextFactory;
            }
    
            public void Initialize()
            {
               using (var umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
                {                   
                    var contentCache = umbracoContextReference.UmbracoContext.ContentCache;
    
                    //get IPublishedContent
                    IPublishedContent particularItem = contentCache.GetById(node.Id);
                    string url = particularItem.UrlAbsolute();
                }
    
            }       
    
            public void Terminate()
            {
                throw new NotImplementedException();
            }
        }
    }
    
  • Christian John Schmidt 6 posts 106 karma points
    Apr 09, 2019 @ 05:27
    Christian John Schmidt
    0

    This is exactly the solution i was already using! However for me the UrlAbsolute method only returned the relative path.

    I found out that the reason for this, was that I was working locally and therefore did not set the domain in culture and hostnames

  • Chris Scheidtmann 27 posts 139 karma points c-trib
    Jul 11, 2019 @ 08:09
    Chris Scheidtmann
    6

    Note that as of version 8.1 .UrlAbsolute() has been removed and I found one can now use .Url(mode: UrlMode.Absolute).

    I could not find any reference to this in the release notes / fixed issues list, so I had to look at the history for PublishedContentExtensions.cs and found this commit which removed it: https://github.com/umbraco/Umbraco-CMS/commit/11ef00c63d9773fccf1d58897a74f7808c428760#diff-182d0a3c19feafdbcc33e1798ad57511 further down I noticed this change to UmbracoContext.cs which led me to the solution: [Obsolete("Use the Url() method with UrlMode.Absolute.")]

  • Luke Hook 38 posts 151 karma points
    Jul 11, 2019 @ 09:05
    Luke Hook
    1

    For reference, after searching myself I did find this within the release notes but bundled under IPublishedContent refurb

    Refurbish IPublishedContent

    Here is an extracted summary of explicit changes from Stephan.

    The following IPublishedContent members change:

    Name: content.Name is gone, content.GetCulture(...).Name is gone, the proper way to get a name is content.Name(string culture = null)

    UrlSegment: content.UrlSegment is gone, content.GetCulture(...).UrlSegment is gone, the proper way to get a Url segment is content.UrlSegment(string culture = null)

    ItemType: content.ItemType is gone, the proper way to get the item type is content.ContentType.ItemType

    Culture: content.Cultures now returns a collection of string representing the available cultures, and content.GetCulture(...) is gone, the proper way to get the culture date is content.CultureDate(string culture = null)

    Parent: content.Parent is gone, the proper way to get the parent is content.Parent()

    Children: content.Children is gone, the proper way to get the children is content.Children(string culture = null) and it always filters children according to culture

    Url: content.Url, .GetUrl(...), .Url(), .UrlAbsolute() are all gone, the proper way to get the Url is content.Url(string culture = null, UrlMode mode = UrlMode.Auto) which produces a relative-or-absolute Url depending on what's "best", but can be forced to produce absolute Urls with UrlMode.Absolute

    (Apologies that's not very easy to read)

    Definitely think this needed to be made more obvious within the release notes, especially if the documentation isn't updated yet.

  • Chris Scheidtmann 27 posts 139 karma points c-trib
    Jul 11, 2019 @ 11:28
    Chris Scheidtmann
    0

    I see, thanks for referring to that ticket. The details are buried in the comments, so I really feel like this should be detailed a bit more in the version-specific upgrade guide.

Please Sign in or register to post replies

Write your reply to:

Draft