sotirisf / Umbraco-VirtualNodes

Lets you specify one or more document types that will be excluded from Umbraco-generated URLs, thus making them "invisible". Those can be used as grouping nodes and they will not appear as part of the URL.
MIT License
16 stars 7 forks source link

Compatible with GetByRoute? #13

Open dnwhte opened 4 years ago

dnwhte commented 4 years ago
UmbracoContext.ContentCache.GetByRoute

Always returns null when requesting a url that contains (or does not contain rather) a virtual node.

For example,

UmbracoContext.ContentCache.GetByRoute("/section-1") // returns null
UmbracoContext.ContentCache.GetByRoute("/sections/section-1") // returns section-1 node

Any idea what's up with this? Or how to make it work with GetByRoute? Or an alternative w/o traversing the tree?

sotirisf commented 4 years ago

Unfortunately it doesn't seem to work with GetByRoute().

I tried adding a custom route but GetByRoute won't pick it up. Other workarounds like using a custom content finder or manually registering a redirect won't work either (had to try though! :) )

Any ideas welcome.

dnwhte commented 4 years ago

Still no luck unfortunately. I ended up copying a lot of the code from the VirtualNodesContentFinder for my purposes, so I can at least utilize the cache.

I found that, for my case at least, GetSingleByXPath is faster than the query used at https://github.com/sotirisf/Umbraco-VirtualNodes/blob/master/VirtualNodes/VirtualNodesContentFinder.cs#L50. I'm on v7 though. I don't know that it will be the case on v8.

My method:

IEnumerable<string> pathSegments = contentRequest.Uri.AbsolutePath.Split('/').Where(s => !string.IsNullOrWhiteSpace(s));
string xpath = "/";
foreach (string seg in pathSegments)
{
    xpath += "/*[@urlName='" + seg + "'][1]";
}
IPublishedContent item = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetSingleByXPath(xpath);