Adding synamic SiteMapNode

Last post 09-01-2006 9:19 PM by MuteThis. 2 replies.

Sort Posts:

  • Adding synamic SiteMapNode

    09-01-2006, 2:59 PM
    • Member
      10 point Member
    • ursasmar
    • Member since 05-23-2005, 7:51 PM
    • Posts 2

    In my app I have a "details.aspx" page that is used to display product information.  This page is called from a number of different pages, which are parts of different nodes in my sitemap.

     

    Example:

    rootNode

        products

            detail

       search

          detail

    closeNodes

     

    What I would like to happen is that in my breadcrumb trail, it shows the

    Home >> Products >> Details path

    or the

    Home >> Search >> Details path,

    even though this details page is the same file.

     
    I have found that I can't have the Details page listed twice in the sitemap.  And, I don't want to maintain two Details files with different names, but identical code, just so that the sitemap will work.  So, is there a way to add a node dynamically when the Details page is called and assign its parent node the calling page?
     

  • Re: Adding synamic SiteMapNode

    09-01-2006, 8:04 PM
    • Participant
      1,114 point Participant
    • MuteThis
    • Member since 09-21-2003, 9:10 AM
    • Cincinnati, Ohio
    • Posts 208

    What I am about to suggest amounts to what some (myself included) would call a hack...  I'm sure there has to be a better way, but I haven't really played with the built-in navigation stuff.  Here it goes..

    My solution has three site map files.  One for default where the details page is not included "default", a second where the details page is a child of products "FromProducts" and a third where the details page is a child of search "FromSearch".

    A little bit of configuration information needs to be added to the web.config file to take this approach.

    <siteMap defaultProvider="Default">
    	<providers>
    		<add 
    		  name="Default"
    		  type="System.Web.XmlSiteMapProvider" 
    		  siteMapFile="~/Web.sitemap" />
    		<add 
    		  name="FromProduct"
    		  type="System.Web.XmlSiteMapProvider" 
    		  siteMapFile="~/FromProduct.sitemap" />
    		<add 
    		  name="FromSearch"
    		  type="System.Web.XmlSiteMapProvider" 
    		  siteMapFile="~/FromSearch.sitemap" />
    	</providers>
    </siteMap>

     And now the code to make it happen on the details page.  For brevities sake, I'm using this code inline, ideally you'd put it in a method.

    protected void Page_Init(object sender, EventArgs e)
    {
    	string Referrer = (Request.UrlReferrer == null) ? null : Request.UrlReferrer.AbsolutePath.ToLower();
    
    	if (string.IsNullOrEmpty(Referrer) == true)
    	{
    		Response.Redirect("~/Default.aspx");
    	}
    	else if (Referrer.EndsWith("products.aspx") == true)
    	{
    		SiteMapPath1.SiteMapProvider = "FromProduct";
    	}
    	else if (Referrer.EndsWith("search.aspx") == true)
    	{
    		SiteMapPath1.SiteMapProvider = "FromSearch";
    	}
    }

    That's it...

     You are correct, this solution is less than ideal, but it was my first stab at a solution.  I'll hack around at dynamically adding details to the SiteMapPath and post again if I have any luck.

     

    Ben
    MCAD .Net (70-315, 70-320 & 70-229)
    :One that throws dirt loses ground:
  • Re: Adding synamic SiteMapNode

    09-01-2006, 9:19 PM
    Answer
    • Participant
      1,114 point Participant
    • MuteThis
    • Member since 09-21-2003, 9:10 AM
    • Cincinnati, Ohio
    • Posts 208

    Ok, here is something a little better.  I can't say I'm happy with it either...

    You need to wire up an event in Global.asax

    void Application_Start(object sender, EventArgs e) 
    {
    	// Code that runs on application startup
    	SiteMap.SiteMapResolve += (SiteMapHandler.SiteMap_SiteMapResolve);
    }

    And now some code for your App_Code directory: SiteMapHandler.cs

    using System;
    using System.Web;
    
    /// <summary>
    /// Summary description for SiteMapHandler
    /// </summary>
    public class SiteMapHandler
    {
    	public static SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
    	{
    		SiteMapNode TempNode = new SiteMapNode(SiteMap.Provider, "Details.aspx", "~/Details.aspx", "Details");
    
    		string Referrer = (e.Context.Request.UrlReferrer == null) ? null : e.Context.Request.UrlReferrer.AbsolutePath.ToLower();
    
    		if (string.IsNullOrEmpty(Referrer) == true ||
    			e.Context.Request.Url.AbsolutePath.ToLower().EndsWith("/detail.aspx") == false)
    		{
    			return null;
    		}
    		else
    		{
    			TempNode.ParentNode = SiteMap.Provider.FindSiteMapNode(Referrer);
    		}
    
    		return TempNode;
    	}
    }
    

     

    Enjoy

    Ben
    MCAD .Net (70-315, 70-320 & 70-229)
    :One that throws dirt loses ground:
Page 1 of 1 (3 items)