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

发布于 2024-12-20 03:17:22 字数 2686 浏览 1 评论 0原文

我正在使用 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 存在于我的站点地图文件中时节点变得为空。请建议我正确的解决方案。

i am working with asp.net 4.0 webform project. when i am accessing a then site map title is not showing for that page.

here is my site map file detail

<?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>

i am accessing UPSLabelFormUK.aspx like localhost:4990/UK/Label then some time title is displaying on the page and sometime not. page is coming because i wrote routing code for that like

 void Application_Start(object sender, EventArgs e)
  {

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

i debug the code and found that sometime node is being null.
here is the line in SiteMapResolve event

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

my partial code in sitemap resolve event look like

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);
              }
            }
          }

can anyone tell me why node is getting null when virtual path return ~/UPS/UPSLabelFormUK.aspx and this url exist in my sitemap file. please suggest me the right solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文