Is it possible use ASP.NET Sitemap to generate Breadcrumbs?

abatishchev picture abatishchev · Feb 16, 2009 · Viewed 8.8k times · Source

I want my ASP.NET site to have simple menu string aka Breadcrumbs. I have created Sitemap with all required elements and registered into Web.config. For example:

<siteMap>
    <siteMapNode url="Default.aspx" title="Home" >
        <siteMapNode url="hosting/Default.aspx" title="Hosting" />
        <siteMapNode url="software/Default.aspx" title="Software">
            <siteMapNode url="firefox/Default.aspx" title="Firefox">
                <siteMapNode url="Download.aspx" title="Download" />
                <siteMapNode url="Support.aspx" title="Support" />
            </siteMapNode>
        </siteMapNode>
    </siteMapNode>
</siteMap>

And created a control placed on Masterpage. Here it's menu generation code:

protected void Control_Load(Object sender, EventArgs e)
{
    string path = String.Empty;
    StringCollection list = new StringCollection();

    foreach (string str in Request.Url.Segments)
    {
        path += str;
        string link = String.Format("<a href=\"{0}://{1}{2}\">{3}</a>", Request.Url.Scheme, Request.Url.Authority, path, this.names[str]);
        list.Add(link);
    }

    foreach (string str in list)
    {
        menu += String.Concat(str, SeparatorLine);
    }
    menu = menu.Remove(menu.LastIndexOf(SeparatorLine));
}

But it uses a StringDictionary like { { "/", "Home" }, { "hosting/", "Hosting" }, { "software/", "Software" } .. }

How can I use a query to Sitemap instead of it? Or maybe something else, not Sitemap, but beforehand invented.

Answer

Rex M picture Rex M · Feb 16, 2009

ASP.NET SiteMapPath Control

<asp:SiteMapPath ID="SiteMapPath1" Runat="server" />