I created a VirtualPathProvider to stream code from a database and needed to dynamically create siteMapNodes, with custom attributes, to use throughout my site. They were used in a breadcrumb and also to provide some information within the master page and in user controls. So, I did some research and found the SiteMap.SiteMapResolve Handler to deal with any virtual pages that weren't in the SiteMap.
It is dealt with in the global.asax:-
Code:Sub Application_Start(ByVal sender AsObject, ByVal e As EventArgs) ' Code that runs on application startup 'this initializes the VirtualPathProvider. It should not be needed for the SiteMap element HostingEnvironment.RegisterVirtualPathProvider(New myVirtualProvider()) 'adds the handler for creating siteMapNodes for virtual paths AddHandler SiteMap.SiteMapResolve, AddressOf SiteMap_SiteMapResolve EndSub
siteMapResolve function
Code:Private Function SiteMap_SiteMapResolve(ByVal sender As Object, ByVal e As SiteMapResolveEventArgs) As SiteMapNode 'check if the CurrentNode exists in the SiteMap If SiteMap.CurrentNode Is Nothing Then 'Dim newNode As XmlElement 'Dim arr_script_name As Array = Split(HttpContext.Current.Request.ServerVariables("SCRIPT_NAME"), "/") Try 'retrieve the path of the virtual file from the url Dim pagePath As String = Replace(HttpContext.Current.Request.Url.ToString, "http://www.domain.com", "") 'get the virtual file details from the db 'code is my own object that contains the details of the virtual file Dim code As codes = codes.GetCodeFile(pagePath) 'split the codefile property to enable us to retrieve the page name Dim arrCodeFile As Array = Split(code.codeFile, "/") 'retrieve the page name from the array that is the last item Dim codeFile As String = arrCodeFile(UBound(arrCodeFile)) 'create a new siteMapNode element on the SiteMap using the page name as the key Dim nNewNode As New SiteMapNode(e.Provider, "/" & codeFile) 'clone the node to make it editable nNewNode.Clone(True) 'add the parentNode property by finding it by key from the site map file 'this only works if you're adding the virtual file as a child of a physical file nNewNode.ParentNode = SiteMap.Provider.FindSiteMapNodeFromKey("/" & code.parentNode) 'set the default properties of the siteMapNode nNewNode.Url = codeFile nNewNode.Title = code.title nNewNode.Description = code.SMdescription 'you can also add custom attributes to the siteMapNode nNewNode("link") = code.codeFile nNewNode("pageLanguage") = code.SMpageLanguage nNewNode("left_menu") = code.SMleft_menu 'return the new siteMapNode Return nNewNode Catch ex As Exception Throw New Exception(ex.ToString) End Try End If 'just return the standard CurrentNode if the file is physical and exists in the SiteMap Return SiteMap.CurrentNode End Function
You need to have a seperate property in your virtual file system for each attribute you want to include in your siteMapNode. You also need to have a parentNode property that is a physical file, ie not another virtual file, that is the parent of the new node you want to insert. This should be set to just the file name, ie, no ~/ or /
Hope that helps.
if you have any queries, post back and I'll see if I can help...
This is a follow on from another thread I created on VirtualPathProvider, located here....Simple VirtualPathProvider to stream ASP.NET code from a database C#



LinkBack URL
About LinkBacks

Reply With Quote

Bookmarks