Copied to clipboard

Flag this post as spam?

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


  • Chris Houston 535 posts 980 karma points MVP admin c-trib
    Feb 15, 2012 @ 16:45
    Chris Houston
    0

    NiceUrl returns a # instead of the correct URL

    Hi All,

    This seems to be a very similar issue to at least 20 other tickets on the Forum going back at least a year, all of which point to there being some issue with NiceUrl.

    We have recently taken over a site from another agency that was built using Umbraco 4.0.2 and we have upgraded it to 4.7.1.1 this has involved changing all the XSLT to the new format and lot of refactoring along the way.

    The issue we have is within a custom URL Rewriting method, skipping a few steps this does the following:

    var newNode = new umbraco.NodeFactory.Node(int.Parse(nodeId.Value));
    ourNiceUrl = newNode.NiceUrl;

    And we have also tried to change this to work in this way:

    var myUrl = library.NiceUrl(int.Parse(nodeId.Value));

    Both of the above should return a nice URL but instead they both return a #

    This is where it gets weird... if we publish the page we are trying to access and attempt to access the same page again, it works as expected.

    However, if we restart IIS ( or re-build our project and deploy the new code, essentially restarting the site ) we get back to the issue of NiceUrl returning a # symbol.

    Does anyone have any ideas bar us having to debug the core?

    Best regards,

    Chris

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Feb 15, 2012 @ 16:49
    Ismail Mayat
    0

    Chris,

    Are permissions correct and is the site running in its own app pool and is the account the app pool is running under the one that has folder access?

    Regards

    Ismail

  • Chris Houston 535 posts 980 karma points MVP admin c-trib
    Feb 15, 2012 @ 17:06
    Chris Houston
    0

    Hi Ismail,

    Yes, it's currently configured to run using the Network Service account.

    If you step through, the newNode object exists, it's the right page node, but the NiceUrl is '#'

    As you can see from the screen shot below...

    But, as mentioned above, as soon as we re-publish the page, everything just magically starts to work, I have a dent in my head from banging the wall so much with this one!

    Cheers, Chris

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Feb 15, 2012 @ 17:24
    Dirk De Grave
    0

    Chris,

    Seen that issue before on our sites... unless we reference a property on the object before using NiceUrl property... pretty weird... So, in code, try to get a property first and then try NiceUrl on the object. Does it work then? Ok, this is not a solution, but in these rare cases, i use umbraco.library.NiceUrl(node.Id) to generate the url.

     

    Cheers,

    /Dirk

  • Chris Houston 535 posts 980 karma points MVP admin c-trib
    Feb 15, 2012 @ 19:38
    Chris Houston
    0

    Hi Dirk,

    We have tried that too ( referencing a property )

    We have also tried the Umbraco.library.NiceUrl(node.id)  ( I did mention that in the first post :) ) but that also returns the '#'

    Hence it's a real head banger,

    What I don't get, is why does re-publishing a page make things suddenly work?! Another weird thing, is that if you re-publish the site it does not fix things, if you publish the parent node it does not fix things... you have to actually publish the individual page that is not working and then ping.... it works.

    Cheers,

    Chris

  • Chris Houston 535 posts 980 karma points MVP admin c-trib
    Feb 16, 2012 @ 04:22
    Chris Houston
    0

    Well, as so far no one seems to know the answer to this, I've downloaded the Umbraco 4.7.1 source...

    I have modified the NiceUrl method so that instead of catching and ignoring errors, it now logs them to the umbracoLog table like so:

            public static string NiceUrl(int nodeID)
    {
    try
    {
    int startNode = 1;
    if (GlobalSettings.HideTopLevelNodeFromPath)
    startNode = 2;

    return niceUrlDo(nodeID, startNode, false);
    }
    catch (Exception ex)
    {
    Log.Add(LogTypes.Error, new User(0), nodeID, ex.ToString());
    return "#";
    }
    }

     

    As you can see I also left in the default behaviour, which is to return a '#' if there is an error, so we now all know where these '#' symbols are coming from :)

    The error that is being logged is:

     System.NullReferenceException: Object reference not set to an instance of an object. 
     at umbraco.library.niceUrlJuno(Int32 nodeId, Int32 startNodeDepth, String currentDomain, Boolean forceDomain)
     in g:\2 - Projects\Umbraco471Source\umbraco\presentation\library.cs:line 441    
     
     at umbraco.library.NiceUrlFetch(Int32 nodeID, Int32 startNodeDepth, Boolean forceDomain)
     in g:\2 - Projects\Umbraco471Source\umbraco\presentation\library.cs:line 524    
     
     at umbraco.library.niceUrlDo(Int32 nodeID, Int32 startNodeDepth, Boolean forceDomain) 
     in g:\2 - Projects\Umbraco471Source\umbraco\presentation\library.cs:line 426    

     at umbraco.library.NiceUrl(Int32 nodeID) in g:\2 - Projects\Umbraco471Source\umbraco\presentation\library.cs:line 372

    So, as you can see the NiceUrl method calls niceUrlDo... which calls NiceUrlFetch.... which in turn calls NiceUrlJuno... which is where our error is occuring.

    The first lines of this method are:

            internal static string niceUrlJuno(int nodeId, int startNodeDepth, string currentDomain, bool forceDomain)
            {
                string parentUrl = String.Empty;
                XmlElement node = UmbracoContext.Current.GetXml().GetElementById(nodeId.ToString());

    And the error is coming from the UmbracoContext.Current as it is equal to NULL !!?!

    So... long story short...  by adding the following to the top of the NiceUrl method:

     if (UmbracoContext.Current == null)
        UmbracoContext.Current = new UmbracoContext(HttpContext.Current);

    And a similar check in the requestModule.cs - Application_Request method:

                // create the Umbraco context
                if (UmbracoContext.Current == null)
                    UmbracoContext.Current = new UmbracoContext(httpContext);

    This fixes this bug :)

    Can anyone see any issues with those changes as everything seems to work fine?

    The reason for the issue is that the UmbracoContext does not get created until the Application_Request method fires and this is AFTER the URL Redirect module tries to do it's re-directing and hence there is no Context.

    Cheers all,

    Chris

Please Sign in or register to post replies

Write your reply to:

Draft