I ma tyring to display the entire URL of a page, including host name. From the docs I am supposed to be able to do @Model.Url(PublishedUrlProvider) but when I do that I am told that I PublishedUrlProvider does not exist, even after adding Umbraco.Cms.Core.Routing as suggested by Visual Studio. I tried @Model.Url() but that does not include the host name. What is the right code to be using and what should I be including/injecting to the razor page?
You want to use the below. UrlMode
should be accessible, but if not you can find it in the namespace Umbraco.Cms.Core.Models.PublishedContent
.
@Model.Url(mode: UrlMode.Absolute)
Might be more tricky when you have multiple domains configured.
If you’d like to display all links, then you have to figure out what’s assigned to your node like following:
var domains = UmbracoContext.Domains.GetAssigned(page.Id, false);
if (domains == null || !domains.Any())
domains = UmbracoContext.Domains.GetAssigned(root.Id, false);
and to display all urls of the page you have to iterate over the above results and concatenate with relative url like following:
var relativeUrl = page.Url(mode: UrlMode.Relative);
<ul>
@foreach (var domain in domains)
{
var url = $"{Context.Request.Scheme}://{domain.Name}/{relativeUrl}";
<li>
<a href="@(url)" target="_blank">@(url)</a>
</li>
}
</ul>
The UrlMode.Absolute will do what you want, but I think you needed the IPublishedUrlProvider (the interface) instead of PublishedUrlProvider.
That looks to be working. I did at one point try @Model.Url(UrlMode.Absolute.ToString())
[had to add the ToString as without it VS complained about a conversion problem] but that only produced a hash tag. Thanks for clearing this up for me.