Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • tentonipete 78 posts 223 karma points
    Mar 12, 2012 @ 14:00
    tentonipete
    0

    uQuery.GetNodeByUrl() usage error/bug?

    Hi guys!

    I'm using Umbraco 4.7.1.1 & uComponents 3.0.2

    I'm using the uQuery.GetNodeByUrl() method and getting an interesting XPath related error. This is just a quick check to make sure i'm using it properly!

    Node n = uQuery.GetNodeByUrl("/test.aspx");

    This returns an XPathException stating "Expression must evaluate to a node-set."

    Am I using this method properly? test.aspx exists and I can browse to it! Prefixing the url with "~" doesn't help either.

    Could it be a bug?

    Thanks for your help

  • Lee Kelleher 3876 posts 14590 karma points MVP 9x admin c-trib
    Mar 12, 2012 @ 14:39
    Lee Kelleher
    1

    Hi Joel,

    Not sure what is going on there.

    I've just re-tested this with a fresh v4.7.1.1. install and uComponents v3.0.2.  On the homepage template, added in an inline script...

    <script runat="server">
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
    
            var node = uComponents.Core.uQuery.GetNodeByUrl("/installing-modules.aspx");
            Response.Write(node.Id);
        }
    </script>

    Which outputted the id as 1049.  When I changed the URL to something else, (that didn't exist), that returned a -1.

    If you've got time and want to help get to the bottom of this, try either downloading the symbol files, or the source (for v3.0.2) to see if there is an underlying exception.  Would be good to iron out any problems (that I can't reproduce).

    Thanks, Lee.

  • tentonipete 78 posts 223 karma points
    Mar 12, 2012 @ 14:49
    tentonipete
    1

    Thanks, Lee.

    Have noticed that it is ocurring intermittently which suggests there could be some other foul play afoot...

    Will investigate further and dig deeper to uncover the villain responsible!

    Joel

  • tentonipete 78 posts 223 karma points
    Mar 13, 2012 @ 15:53
    tentonipete
    0

    I'm calling the method in an HttpHandler and the error only occurs when I've recycled the application pool and I haven't already navigated to that page.

    I'm guessing that something in Umbraco is not loaded into memory until i've hit an aspx page...

  • Lee Kelleher 3876 posts 14590 karma points MVP 9x admin c-trib
    Mar 13, 2012 @ 16:40
    Lee Kelleher
    0

    From looking at the uQuery code, we are querying the content XML cache.  Which suggests that until you "start-up" Umbraco, the XML cache isn't initialised.

    Wondering if there is another API call you could make to "warm it up"?

    Cheers, Lee.

  • Hendy Racher 859 posts 3821 karma points MVP admin c-trib
    Mar 13, 2012 @ 17:35
    Hendy Racher
    2

    Hi, there was a post last week about refreshing the xml file, I wonder if the following will do the trick ?

    umbraco.library.RefreshContent();
    

     

  • tentonipete 78 posts 223 karma points
    Mar 13, 2012 @ 18:13
    tentonipete
    0

    Cool. Isn't that called in Application_Start?!

    Guessing that RefreshContent() is pretty expensive so I probably don't want to be doing it every time on the off chance that it's not warmed up already. Is there any system variable which can tell me whether content is already loaded into cache or not?

    We're probably getting into realms outside of uComponents now, but thanks for the input guys!

  • Lee Kelleher 3876 posts 14590 karma points MVP 9x admin c-trib
    Mar 13, 2012 @ 19:29
    Lee Kelleher
    1

    Just been looking over the core (like I spend most of my Tuesday afternoons) ... and reckon you should be able to do a quick check on the content cache.

    if (umbraco.content.Instance != null) {

    Tthe "umbraco.content" has a static constructor which will perform a check called "CheckXmlContentPopulation()" ... if the content isn't already initialised, it will do so.

    ... it's so crazy, that it. might. just. work! ;-)

    Cheers, Lee.

  • IRM 1 post 25 karma points
    Sep 21, 2012 @ 13:14
    IRM
    4

    I encountered this problem in Umbraco 4.9. After digging in the source code, I found that the cause is a bug in the umbraco.requestHandler class, which is used internally by uQuery.GetNodeByUrl to do the heavy lifting.

    requestHandler has a static method CreateXPathQuery. If this is called before any instances of requestHandler have been created, then the method will return a malformed XPath query. For example, 

    requestHandler.CreateXPathQuery("root/test-page/", true)

    will return

    "/root/*/* [ = \"root\"]/* [ = \"test-page\"]"

    when it should return

     "/root/*/* [@urlName = \"root\"]/* [@urlName = \"test-page\"]"

    This is the cause of uQuery.GetNodeByUrl failing.

    The reason for CreateXPathQuery returning a broken XPath is that requestHandler has a static method "InitializeUrlName" which is only called by its constructor, hence the bug only occurs if the uQuery API calls are made before any requests have been handled by the Umbraco site, e.g. immediately after an application pool recycle.

    My workaround is to use reflection to ensure that requestHandler is properly initialized:

    public Node GetNode(string path)
    {
    Node node = null;
    try
    {
    node = umbraco.uQuery.GetNodeByUrl(path);
    }
    catch (XPathException)
    {
    WorkaroundForUmbracoRequestHandlerBug();
    node = umbraco.uQuery.GetNodeByUrl(path);
    }
    return node;
    }

    private static void WorkaroundForUmbracoRequestHandlerBug()
    {
    Type requestHandlerType = typeof(requestHandler);
    System.Reflection.MethodInfo method = requestHandlerType.GetMethod(
    "InitializeUrlName", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
    method.Invoke(null, null);
    }
Please Sign in or register to post replies

Write your reply to:

Draft