U4-1489 - System.Web.HttpContext.Current.Items["pageID"] is null

Created by Jeroen Breuer 17 Jan 2013, 12:39:00 Updated by Shannon Deminick 26 Jun 2017, 05:41:32

Relates to: U4-5417

I'm currently working on a website where I have the complete umbraco source (4.11.1) running in debug mode so I can see what happens. When I'm visiting a page I notice that getCurrentNodeId() in Node.cs is called 3 times. Here is the code: public static int getCurrentNodeId() { XmlNode n = ((IHasXmlNode)library.GetXmlNodeCurrent().Current).GetNode(); if (n.Attributes == null || n.Attributes.GetNamedItem("id") == null) throw new ArgumentException("Current node is null. This might be due to previewing an unpublished node. As the NodeFactory works with published data, macros using the node factory won't work in preview mode.", "Current node is " + System.Web.HttpContext.Current.Items["pageID"].ToString());

return int.Parse(n.Attributes.GetNamedItem("id").Value);

} It can't find a node so it tries to throw the ArgumentException, but because System.Web.HttpContext.Current.Items["pageID"] is null it will throw a "object reference not set to an instance of an object" error on .ToString(). There are no items in System.Web.HttpContext.Current.Items. When I debug I get the exception 3 times, but the page still renders without error. I'm working in MVC and I don't use the Node object anywhere. Instead I use the IPublishedContent. Any idea why this might happen?

Comments

Jeroen Breuer 27 May 2013, 13:07:57

I made a small update to the getCurrentNodeId method so I don't get this exception while debugging. public static int getCurrentNodeId() { XmlNode n = ((IHasXmlNode)library.GetXmlNodeCurrent().Current).GetNode(); if (n.Attributes == null || n.Attributes.GetNamedItem("id") == null) { string id = ""; try catch (Exception ex) throw new ArgumentException("Current node is null. This might be due to previewing an unpublished node. As the NodeFactory works with published data, macros using the node factory won't work in preview mode.", "Current node is " + id); }

return int.Parse(n.Attributes.GetNamedItem("id").Value);

}


Shannon Deminick 26 Jun 2017, 05:41:32

Closing issue due to inactivity - see blog post for details https://umbraco.com/blog/issue-tracker-cleanup/


Priority: Normal

Type: Bug

State: Closed

Assignee:

Difficulty: Normal

Category:

Backwards Compatible: True

Fix Submitted:

Affected versions: 4.11.1

Due in version:

Sprint:

Story Points:

Cycle: