Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SiteMapResolve does not fire

Tags:

asp.net

I am getting trouble that SiteMapResolve fires on some pages and doesn't on other.

This is my code.

protected void Page_Load(object sender, EventArgs e)
{
     SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(this.ChangeMapPath);
}

private SiteMapNode ChangeMapPath(Object sender, SiteMapResolveEventArgs e)
{
    if (SiteMap.CurrentNode != null)
    {
        // Clone the current node and all of its relevant parents.
        SiteMapNode currentNode = SiteMap.CurrentNode.Clone(true);
        SiteMapNode tempNode = currentNode;

        if (clientId != 0 && tempNode.Title.Equals("Client Notes"))
        {
            tempNode.Url = tempNode.Url + EncryptQueryString("ParentId=" + clientId.ToString());
        }
        if (clientId != 0 && tempNode.ParentNode != null && (tempNode.ParentNode.Title.Equals("Client Contacts")))
        {
            tempNode.ParentNode.Url = tempNode.ParentNode.Url + EncryptQueryString("ParentId=" + clientId.ToString());
        }
        else if (tempNode.ParentNode != null)
            tempNode.ParentNode.Url = tempNode.ParentNode.Url;

        return currentNode;
    }
    return null;
}

Thanks.

like image 353
Sami Avatar asked Apr 07 '26 04:04

Sami


2 Answers

Try replace your code

SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(this.ChangeMapPath);

to

foreach (SiteMapProvider mapProvider in SiteMap.Providers)
{
    mapProvider.SiteMapResolve += ChangeMapPath;
}
like image 86
Alexander Shashkin Avatar answered Apr 10 '26 01:04

Alexander Shashkin


I know this is an old question, but just in case anyone else is still dealing with this issue, the following solution works for me.

It appears that there is an issue with SiteMapResolveEventHandler not ever being removed, so the first event handler that is added, will be the one that gets called after that point.

https://blogs.msdn.microsoft.com/asiatech/2012/11/29/asp-net-case-study-sitemapresolveeventhandler-memory-leakage/

So, you can do exactly as the original poster has done, but you also need to add the following to each page that adds the SiteMapResolveEventHandler.

protected void Page_UnLoad(object sender, EventArgs e)
{
    SiteMap.Provider.SiteMapResolve -= new SiteMapResolveEventHandler(this.ChangeMapPath);
}

It should call the appropriate handler on each page if you have forced the removal on previous pages.

Alternatively, instead of using the Page_Unload, you could place the

SiteMap.Provider.SiteMapResolve -= new SiteMapResolveEventHandler(this.ChangeMapPath); 

line of code at the beginning of the ChangeMapPath method.

like image 41
user2023116 Avatar answered Apr 09 '26 23:04

user2023116