开发者

SiteMapResolve does not fire

开发者 https://www.devze.com 2023-03-25 21:52 出处:网络
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)

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 &开发者_高级运维amp;& (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.


Try replace your code

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

to

foreach (SiteMapProvider mapProvider in SiteMap.Providers)
{
    mapProvider.SiteMapResolve += ChangeMapPath;
}


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.


What worked for me better and easier to understand / maintain:

Create an interface with a handler method:

public interface ISiteMapResolver
{
    SiteMapNode SiteMapResolve(object sender, SiteMapResolveEventArgs e);
}

subscribe to each event in the Application_Start method in global.asax

foreach (SiteMapProvider siteMapProvider in SiteMap.Providers)
    siteMapProvider.SiteMapResolve += Provider_SiteMapResolve;
}

Then implement this method

SiteMapNode Provider_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
{  
     var handler = e.Context.CurrentHandler as ISiteMapResolver;
         return handler != null ? handler.SiteMapResolve(sender, e) : null;
}

This way you can implement ISiteMapResolverin each page you are interested to modify the node, e.g.

public SiteMapNode SiteMapResolve(object sender, SiteMapResolveEventArgs e)
{
    if (SiteMap.CurrentNode == null) return null;
    var node = SiteMap.CurrentNode.Clone(true);

    node.Url += "?productId=" + ProductId; // Access page property, no problem. Page_Load event already fired.
    note.Title = "Product " + Product;

    // Modify node.ParentNode here if needed.

    return node;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消