Copied to clipboard

Flag this post as spam?

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


  • cardinal252 21 posts 33 karma points
    Oct 26, 2009 @ 12:32
    cardinal252
    0

    Getting a node id from its url

    Hey Folks

    I am currently trying to implement a custom not found handler for a given set of urls. The redirect page is to change dependant on what level the requested page should have been on (in essence there is going to be a default page that can be overridden if the correct node ACTUALLY exists)

    As such, I am trying to get the node id for the default pages to act as the redirect node id when the handler is utilised, as the node id may well be different between staging and live or may change, I am trying to do this using the url of the node.

    How can I achieve this?

    Thanks in advance

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Oct 26, 2009 @ 14:15
    Lee Kelleher
    0

    Hi cardinal252,

    You could try something funky by using the CreateXPathQuery method in umbraco.requestHandler?

    For example:

    public static Int32 GetNodeIdFromUrl(String url)
    {
        Int32 nodeId = -1;
        String xpath = umbraco.requestHandler.CreateXPathQuery(url, false);
    
        if (!String.IsNullOrEmpty(xpath))
        {
            System.Xml.XmlNode node = umbraco.content.Instance.XmlContent.SelectSingleNode(xpath);
            if ((node != null) && (node.Attributes != null))
            {
                Int32.TryParse(node.Attributes["id"].Value, out nodeId);
            }
        }
    
        return nodeId;
    }

    I haven't given this code much testing, so you should play around with various properties to get the best results.  I'd suggest passing through the URL parameter without the domain prefix.

    Good luck, and let us know how it works out!

    Cheers, Lee.

Please Sign in or register to post replies

Write your reply to:

Draft