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();
}
}
}
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.
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.
Url with domain in Umbraco 8
In order to get an url with domain in V7 you could do like this:
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
Maybe you are searching for this method?
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:
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
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 toUmbracoContext.cs
which led me to the solution:[Obsolete("Use the Url() method with UrlMode.Absolute.")]
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.
(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.
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.
why is this so complicated how can i read IPublishedContent URL on umbraco 8 what extension are they taking about ?
Instead of the property
.Url
you use the method.Url()
(which has parameters forUrlMode
and culture should you need them).is working on a reply...