SiteMapResolve Flaw

Last post 03-20-2008 1:01 PM by jgd12345. 0 replies.

Sort Posts:

  • SiteMapResolve Flaw

    03-20-2008, 1:01 PM
    • Member
      487 point Member
    • jgd12345
    • Member since 06-22-2006, 5:28 PM
    • Posts 267

    Hi, I have problems using the SiteMapResolve event so I found an article http://www.codeproject.com/KB/aspnet/ABetterSiteMapResolve.aspx and used their solution instead.  All was working fine until recently where every now and again the SiteMapResolve event doesn't get called and subsequently the title of the page changes for different requests (if i keep hitting refresh i see the changes).  I checked my code and added a cookie check in the IsSamePage method and my code in the base page but I still get the same problem.  Here's my code:

    /// <summary>
    /// Fires whenever the site map should be resolved.
    /// </summary>
    public event SiteMapResolveEventHandler SiteMapResolve;

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        System.Web.SiteMap.SiteMapResolve += new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        System.Web.SiteMap.SiteMapResolve -= new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);
    }

    protected SiteMapNode SiteMap_SiteMapResolve(object sender, SiteMapResolveEventArgs e)
    {
        // Only raise the event if the request is for this page
        if (this.IsSamePage(Context, e.Context))
            return OnSiteMapResolve(e);

        return System.Web.SiteMap.CurrentNode;
    }

    /// <summary>
    /// Raises the <see cref="SiteMapResolve"/> event for this page.
    /// </summary>
    protected virtual SiteMapNode OnSiteMapResolve(SiteMapResolveEventArgs e)
    {
        if (SiteMapResolve != null)
            return SiteMapResolve(this, e);

        return System.Web.SiteMap.CurrentNode;
    }

    /// <summary>
    /// Determines whether the two contexts are equal and, therefore, whether the SiteMap.SiteMapResolve event should be fired
    /// </summary>
    protected virtual bool IsSamePage(HttpContext context1, HttpContext context2)
    {
        return context1.Request.ServerVariables["HTTP_X_REWRITE_URL"] == context2.Request.ServerVariables["HTTP_X_REWRITE_URL"] && context1.Request.Cookies["SessionID"] != null && context2.Request.Cookies["SessionID"] != null && context1.Request.Cookies["SessionID"] == context2.Request.Cookies["SessionID"];
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (Request.Cookies["SessionID"] == null)
        {
            HttpCookie cookie = new HttpCookie("SessionID");
            cookie.Value = Guid.NewGuid().ToString();
            cookie.Expires = DateTime.Now.AddYears(1);
            Response.Cookies.Add(cookie);
        }
    }

Page 1 of 1 (1 items)