Try our conversational search powered by Generative AI!

GetVirtualpath producing incorrect URLs


Hey Episerver Commerce.

I'm working on an Episerver project, where we are using the Solr Search Provider for indexing. In order to speed up product search, we are indexing the product urls to Solr, so we avoid looking it up in Episerver for each search. This has worked just fine up until now, but for a specific scheduled job, the produced urls have a language specific prefix, which is incorrect. 

Correct cannonical URL: nl-webshop/390699/390700/390716/390731/390732/1000390325/ 
Produced errornous, cannonical URL: nl/nl-webshop/390699/390700/390716/390731/390732/1000390325/

We are using Episerver's standard CachingUrlResolver, which is turn is using the (EpiServer.Web.Routing).DefaultUrlResolver. Our code for fetching the url can be seen in the following line:
_urlResolver.GetVirtualPath(productLink, language, new VirtualPathArguments() { ContextMode = ContextMode.Undefined, ForceCanonical = true })?.VirtualPath;
The method is invoked with the following context: Language = 'nl', EPiServer.Globalization.ContentLanguage.PreferredCulture = 'en', catalog defaultLangauge = 'en'.

Now, GetVirtualPath produces the correct url for all of our scheduled jobs, but one, and as far as I can tell, it is invoked with the exact same arguments and context. 

So, my question is: Does GetVirtualPath take additional context into account that is not specificied as a function of it's arguments, or am I missing some other problem, which can account for this difference in the produced url? 

Thanks in advance for any help! Let me know if further context is needed in order to give an informated response.

Technical information:

Episerver Instance:
CMS: 10.10.6
Commerce: 11.1

All of the scheduled jobs run on the same application server, and Episerver instance. 

Using _urlResovler.GetURL results in the same error.

Edited, Feb 05, 2018 13:55

I actually think that's the correct behavior. The starting point of the routing system is the start page (normally), and /nl/ is the correct path for the NL version. 

Feb 05, 2018 15:43

I actually think that's the correct behavior. The starting point of the routing system is the start page (normally), and /nl/ is the correct path for the NL version. 

Feb 05, 2018 15:43

Hey Quan Mai, thanks for the reponse.

It's not correct in the sense that it doesn't actually correspond to the URL for the product. 

The prefixed URL results in a 404-error, while the non-prefixed URL will route to the product correctly.
This is the case, since the products are rendered on the NL-website:
My current understanding is that the prefix would be required if the domain wasn't nl - that is to say that it would look something along the lines of

I'm still not sure what makes the difference for when the prefix is or isn't added, especially when the arguments for the method are identical.


Edited, Feb 05, 2018 15:48

So are you having sites with different domain, says for en and for nl? 

Feb 05, 2018 15:50

Yes, that is almost correct.

We have one website, but different hostnames for that website with different domains, and different primary cultures.

Specifically, we have the following 4 host names for a single website:

Host Name Culture Type da Primary nl Primary no Primary sv Primary

However, all catalogs are indexed from the same application server. But the indexing scheduled jobs that work, which is to say that they don't add the prefix when calling GetVirtualPath, run on the same Episerver server and instance as the scheduled job that doesn't work. 

Edited, Feb 05, 2018 15:53

I'm guessing here - when it comes to routing it always gets my head spinning, but in the context of a scheduled job it falls back to the root page as the starting point (as SiteDefinition.Current.StartPage would be null at that point). You might want to try to set SiteDefinition.Current.StartPage to correct contentlink at the start of the scheduled job to see if that solves the problem.

Feb 05, 2018 15:59

Hey again. 

I was under the impression that Episerver would use the configured hostnames when fetching the URL for products on a single-site. However, I came to realize that since scheduled jobs run in an anonymous context, the site context actually has to be set manually, otherwise the DefaultUrlResolver will use a standard context in which the prefixes are added to the product URLs, since no hosts exist.

Adding the following line of code for the execution of the scheduled job fixed the problem:

SiteDefinition.Current = _siteDefinitionRepository.List().First();

Thanks for the help.

Feb 06, 2018 15:55
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.