关于asp.net webform项目中的SiteMap.Provider.FindSiteMapNodeFromKey的问题

时间:2011-12-08 14:03:12

标签: asp.net web.sitemap

我正在使用asp.net 4.0 webform项目。当我访问当时的网站地图标题没有显示该页面。

这是我的站点地图文件详细信息

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/index.aspx" title="Home"  description="The WebSite's Home Page">
<siteMapNode url="~/UPS/UPSLabelFormUK.aspx" title="UK Label"  description="UK Label" />
<siteMapNode url="~/ContactUS.aspx" title="ContactUS"  description="Contact US" />
<siteMapNode url="~/KnowledgeBase.aspx" title="Knowledge Base"  description="KnowledgeBase" />
<siteMapNode url="~/PartBase.aspx" title="PartSearch"  description="Part Search" />  
</siteMapNode>
</siteMap>

我正在访问UPSLabelFormUK.aspx,如localhost:4990 / UK / Label,然后有一些时间标题显示在页面上,有时则没有。页面即将到来,因为我为此编写了路由代码

 void Application_Start(object sender, EventArgs e)
  {

    RouteTable.Routes.MapPageRoute("UK_Label", "UK/Label", "~/UPS/UPSLabelFormUK.aspx");
    SiteMap.SiteMapResolve += SiteMap_SiteMapResolve;
  }

我调试代码并发现某个时候节点为空。 这是SiteMapResolve事件中的行

node = SiteMap.Provider.FindSiteMapNode(handler.VirtualPath);

我在站点地图解析事件中的部分代码看起来像

var node = SiteMap.CurrentNode;
    var rc = HttpContext.Current.Request.RequestContext;
    if (rc.HttpContext != null)
    {
      try
      {
        var route = rc.RouteData.Route;
        if (route != null)
        {
          // when node found in sitemap file and url is routing url
          var page = HttpContext.Current.CurrentHandler as System.Web.UI.Page;
          //var segments = route.GetVirtualPath(rc, null).VirtualPath.Split('/');
          //var path = "~/" + string.Join("/", segments.Take(segments.Length - rc.RouteData.Values.Count).ToArray());
          //node = SiteMap.Provider.FindSiteMapNodeFromKey(path);

          if (page != null && page.RouteData != null)
          {
            // if the current page wasn't found in the sitemap, maybe it was becase of routing... let's find out
            if (node == null)
            {
              // See if this page has a route
              var handler = page.RouteData.RouteHandler as PageRouteHandler;
              // if it was a page Route handler, we are in business
              if (handler != null)
              {
                // try and find the virutal path of the .aspx actually handling the request instead.
                node = SiteMap.Provider.FindSiteMapNode(handler.VirtualPath);
              }
            }
          }

当虚拟路径返回〜/ UPS / UPSLabelFormUK.aspx并且我的站点地图文件中存在此URL时,任何人都可以告诉我为什么节点变为空。请建议我正确的解决方案。

0 个答案:

没有答案