Get content absolute URL in background job

Umb V13.7.2, Cultiv Hangfire V3.1.2

I need to create a list of 3 latest news items as a HTML in a background job to send them out as a newsletter.
I need absolute URLs to those news. If I call Umbracos built in method Url() like that:

latestNewsItem.Url("en", UrlMode.Absolute)

I get absolute URL with localhost domain instead of the correct one.

What I’m doing wrong?

I guess that when you usually create Url you have a domain in the request so the Url method takes it from there, but how can I utilise this method in Hangfire background job?

I debugged the UrlProvider.GetUrl and I see that the culture is specified in variationContextAccessor, but umbracoContext doesn’t have the correct url:

There is no way for Umbraco to know what the correct url is without basing it off the request (which from hangfire is a localhost domain).

As you can add several root nodes with several different domains it cannot know which of them you would want.

What you could do is figure it out yourself via code and then use the domainservice to get the domain for that content: Interface IDomainService | Umbraco c# Api docs

But even then that node could have multiple domains assigned, so you would probably have to just pick the first in the list.

1 Like

Since I don’t have absolute domains specified, I think also this will not work.

I have an UmbracoApplicationUrl appsettings option set up, but also this one will not help me, because we have different domain for Publisher node than Subscriber and the scheduled job is running on Publisher.

This means there’s no way that your Publisher instance can know what domain to use. You’ll need to manually set it somewhere.

I’d normally set it on the appropriate node(s) in the backoffice, then look it up programmatically via the domain service as @jemayn recommends.

If you’re only working with one TLD then you could always just just pop it in appsettings. I will often do this in multi-instance sites anyway where the backoffice instance needs to be able to see and/or talk to client instances.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.