Search code examples
.netsitemapproviderbreadcrumbs

How to get information form page in Custom XMLSiteMapProvider


First of all I need to tell you that I use URL Rewriting on this project.

For the article page this is the url : www.mysite.com/section1/section2/month/day/year/modifiedArticleName

For breadcrumbs I use SiteMapPath control with a custom XMLSiteMapProvider because I can't keep all my articles in the xml file. In this provider, in the CurrentNode property, if the url is one of an article, I create a new SiteMapNode, link it to the appropriate parent and return it.

The problem is that I need to provide to that node the article name. I can't get it from the url, because, like you see above, the url uses a modified article name. So I need to get it from the page.

In the CurrentNode property I am able to get an instance of the current running page but, since the article is loaded on Page_Load, I don't have the title yet.

I thought about a solution but I'm not sure exactly how to implement it. So, I should have 2 XMLSiteMapProvider, the default one and my custom one. And use the custom one only on my article page, initializing it only after I load my article details. Can somebody point me to the right direction?

Cheers.


Solution

  • I managed to achieve my goal by doing this:

    In the web.config file:

    <siteMap defaultProvider="RegularXMLSiteMapProvider">
      <providers>
        <add name="RegularXMLSiteMapProvider" type="System.Web.XmlSiteMapProvider" siteMapFile="~/Web.sitemap" />
        <add name="EnhancedXMLSiteMapProvider" type="MyApp.App_Code.EnhancedXMLSiteMapProvider, MyApp" siteMapFile="~/Web.sitemap"/>
      </providers>
    </siteMap>
    

    Whenever I am on a regular page, I use the default provider. When I am on the article page, I do this:

    protected void Page_Load(object sender, EventArgs e)
        {
            LoadArticle();
    
            MasterPages.MyMasterPage myMaster = (MasterPages. MyMasterPage)this.Master;
            myMaster.MySiteMapPath.SiteMapProvider = "EnhancedXMLSiteMapProvider"; 
        }
    

    And finally, in the provider's CurrentNode property I get the article title:

    MyApp.ArticlePage page = (MyApp.ArticlePage)HttpContext.Current.Handler;
                        if (page != null)
                        {
                            if (!string.IsNullOrEmpty(page.Article.Title))
                            {
                                articleName = page.Article.Title;
                            }
                        }