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

雨燕双飞 提交于 2019-12-10 20:27:19

问题


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.


回答1:


ASP.NET SiteMapPath Control

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



回答2:


You can use the SiteMapPath control (should be in the Navigation category of your toolbox). Check this page for a tutorial.




回答3:


Just set the SiteMapProvider property to the provider for the sitemap you want to use for the breadcrumbs and you'll be set. I usually just place the SiteMapPath inside a div and set the CSS on the div to style the breadcrumbs.

One gotcha to look out for though. If you suppress any root nodes in the sitemap, they will still show up in your breadcrumbs. I've run into this by trying to use the same sitemap for breadcrumbs and for the SiteMapDataSource for a treeview where I wanted to not show the starting node.

Good luck!



来源:https://stackoverflow.com/questions/554615/is-it-possible-use-asp-net-sitemap-to-generate-breadcrumbs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!