热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将Web.SiteMap与动态URLS(URL路由)一起使用-UsingWeb.SiteMapwithDynamicURLS(URLRouting)

IwouldliketomatchapproximatematchesinWeb.SiteMap我想在Web.SiteMap中匹配“近似”匹配TheWeb.Sitemap

I would like to match "approximate" matches in Web.SiteMap

我想在Web.SiteMap中匹配“近似”匹配

The Web.Sitemap static sitemap provider works well, except for one thing. IT'S STATIC!

除了一件事之外,Web.Sitemap静态站点地图提供程序运行良好。这是静止的!

So, if I would have to have a sitemapnode for each of the 10,000 articles on my page like so :

所以,如果我必须为我页面上的每篇10,000篇文章都有一个sitemapnode,如下所示:

  • site.com/articles/1/article-title
  • site.com/articles/2/another-article-title
  • site.com/articles/3/another-article-again
  • ...
  • site.com/articles/9999/the-last-article

Is there some kind of Wildcard mapping I can do with the SiteMap to match Anything under Articles?

是否有某种通配符映射我可以使用SiteMap来匹配文章下的任何内容?

Or perhaps in my Webforms Page, is there a way to Manually set the current node?

或者也许在我的Webforms页面中,有没有办法手动设置当前节点?

I've found a "bit" of help on this page when doing this with the ASP.Net MVC Framework, but still looking for a good solution for Webforms.

在使用ASP.Net MVC框架执行此操作时,我在此页面上找到了一些帮助,但仍在为Webforms寻找一个好的解决方案。

I think what I'm going to have to do is create a custom SiteMap Provider

我认为我要做的是创建一个自定义的SiteMap Provider

3 个解决方案

#1


7  

This is in response to the comment above. I can't post the full code, but this is basically how my provider works.

这是对上述评论的回应。我无法发布完整的代码,但这基本上是我的提供商的工作方式。

Suppose you have a page article.aspx, and it uses query string parameter "id" to retrieve and display an article title and body. Then this is in Web.sitemap:

假设您有一个页面article.aspx,它使用查询字符串参数“id”来检索和显示文章标题和正文。然后这是在Web.sitemap中:


Then, you create this class:

然后,您创建此类:

public class DynamicSiteMapPath : SiteMapPath
{
  protected override void InitializeItem(SiteMapNodeItem item)
  {
    if (item.ItemType != SiteMapNodeItemType.PathSeparator)
    {
      string url = item.SiteMapNode.Url;
      string param = item.SiteMapNode["param"];

      // get parameter value
      int id = System.Web.HttpContext.Current.Request.QueryString[param];

      // retrieve article from database using id
      

      // override node link
      HyperLink link = new HyperLink();
      link.NavigateUrl = url + "?" + param + "=" + id.ToString();
      link.Text = ;
      link.ToolTip = ;
      item.Controls.Add(link);
    }
    else
    {
      // if current node is a separator, initialize as usual
      base.InitializeItem(item);
    }
  }
}

Finally, you use this provider in your code just like you would use the static provider.

最后,您在代码中使用此提供程序就像使用静态提供程序一样。


My class is more complicated than this, but these are the basics. Instead of using a querystring parameter, you could just analyze the friendly url you're using, and use that instead to retrieve the correct content. To minimize the additional db lookups with every request, you can add a caching mechanism to the provider (article title usually won't change often).

我的班级比这更复杂,但这些是基础知识。您可以只分析您正在使用的友好URL,而不是使用查询字符串参数,而是使用它来检索正确的内容。要最小化每个请求的额外数据库查找,您可以向提供程序添加缓存机制(文章标题通常不会经常更改)。

Hope this helps.

希望这可以帮助。

#2


2  

This is not entirely an answer to your question I think, but maybe it gives you an idea. I once wrote a DynamicSiteMapPath class inheriting SiteMapPath. I use a custom attribute in each tag in Web.sitemap, like this:

这不完全是我认为的问题的答案,但也许它会给你一个想法。我曾经写过一个继承SiteMapPath的DynamicSiteMapPath类。我在Web.sitemap中的每个 标记中使用自定义属性,如下所示:


Then the DynamicSiteMapPath class gets the "id" parameter value, retrieves the content from the database, and overrides the currently rendered sitemap item node with the correct title and link. It's gonna take a little work, but when done correctly this is a very neat way of providing dynamic page support.

然后,DynamicSiteMapPath类获取“id”参数值,从数据库中检索内容,并使用正确的标题和链接覆盖当前呈现的站点地图项节点。它需要一些工作,但如果正确完成,这是提供动态页面支持的一种非常简洁的方式。

#3


2  

I have been running into this problem and, frankly, not finding any solutions that made me happy... so I borrow ideas from here and there. My solution is multi-part: a) have the SiteMapProvider find the actual page handling the request and use it's node and b) use standard techniques for updating the sitemapnode from there.

我一直在遇到这个问题,坦率地说,没有找到让我开心的任何解决方案......所以我从这里和那里借鉴意见。我的解决方案是多部分:a)让SiteMapProvider找到处理请求的实际页面并使用它的节点,b)使用标准技术从那里更新sitemapnode。

A) The problem I kept running into is that if I didn't have the correct virtual path, the SiteMap.CurrentNode would be null and the fire the SiteMapResolve function. To solve this I subclassed XmlSiteMapProvider and overrode CurrentNode:

A)我一直遇到的问题是,如果我没有正确的虚拟路径,则SiteMap.CurrentNode将为null并触发SiteMapResolve函数。为了解决这个问题,我将XmlSiteMapProvider子类化并覆盖了CurrentNode:

 namespace WebFormTools
{
    class RouteBaseSitemapProvider : XmlSiteMapProvider
    {
        public override SiteMapNode CurrentNode
        {
            get
            {
                var node = base.CurrentNode;


                if (node == null) 
                {
                    // we don't have a node, see if this is from a route
                    var page = HttpContext.Current.CurrentHandler as System.Web.UI.Page;

                    if (page != null && page.RouteData != null)
                    {
                        // try and get the Virtual path associated with this route
                        var handler = page.RouteData.RouteHandler as PageRouteHandler;

                        if (handler != null) {
                            // try and find that path instead.
                            node = FindSiteMapNode(handler.VirtualPath);
                        }
                    }

                }

                return node;
            }
        }
    }
}

Basically if the default implementation doesn't find anything, look up the route (if any) and try to find the node using the handler's virtual path.

基本上,如果默认实现没有找到任何内容,请查找路由(如果有)并尝试使用处理程序的虚拟路径查找节点。

For reference here is part of my Web.Config, Global.asax and SiteMap files:

这里参考我的Web.Config,Global.asax和SiteMap文件的一部分:

Adding the provider

添加提供者

    
  
    
  

The route:

            routes.MapPageRoute("EvalRoutes",
            "Evals/{type}/New.aspx",
            "~/Evals/New.aspx");

And the SiteMap:

和SiteMap:

        

B) I subclass System.Web.UI.Page, aptly named BaseClass, which adds a method to register handlers for the SiteMapResolve event:

B)我子类System.Web.UI.Page,恰当地命名为BaseClass,它添加​​了一个方法来注册SiteMapResolve事件的处理程序:

public System.Web.SiteMapNode Process(System.Web.SiteMapNode currentNode)
    {
        if (currentNode == null) return currentNode;

        var page = HttpContext.Current.CurrentHandler as System.Web.UI.Page;

        if (page != null && page.RouteData != null)
        {

            Dictionary replacements = new Dictionary();

            // build a list of RegEx to aid in converstion, using RegEx so I can ignore class.  Technically I could also
            foreach (var key in page.RouteData.Values.Keys)
            {
                replacements.Add(new Regex(string.Format("\\{{{0}\\}}", key), RegexOptions.IgnoreCase), page.RouteData.Values[key].ToString());              
            }


            // navigate up the nodes
            var activeNode = currentNode;
            while (activeNode != null)
            {
                // to the replacements
                foreach(var replacement in replacements)
                {
                    activeNode.Title =  replacement.Key.Replace(activeNode.Title, replacement.Value);
                }

                activeNode = activeNode.ParentNode;
            }

        }

        return currentNode;
    }

I still need to have the URLs map appropriately (the would use the URL of the page receiving the route), which is sans routing information. I will probably use a custom Attribute in the sitemap to tell the node how to render the URL.

我仍然需要适当地映射URL(将使用接收路由的页面的URL),这是没有路由信息。我可能会在站点地图中使用自定义属性来告诉节点如何呈现URL。


推荐阅读
  • PDO MySQL
    PDOMySQL如果文章有成千上万篇,该怎样保存?数据保存有多种方式,比如单机文件、单机数据库(SQLite)、网络数据库(MySQL、MariaDB)等等。根据项目来选择,做We ... [详细]
  • 本文介绍了如何使用PHP向系统日历中添加事件的方法,通过使用PHP技术可以实现自动添加事件的功能,从而实现全局通知系统和迅速记录工具的自动化。同时还提到了系统exchange自带的日历具有同步感的特点,以及使用web技术实现自动添加事件的优势。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了设计师伊振华受邀参与沈阳市智慧城市运行管理中心项目的整体设计,并以数字赋能和创新驱动高质量发展的理念,建设了集成、智慧、高效的一体化城市综合管理平台,促进了城市的数字化转型。该中心被称为当代城市的智能心脏,为沈阳市的智慧城市建设做出了重要贡献。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • springmvc学习笔记(十):控制器业务方法中通过注解实现封装Javabean接收表单提交的数据
    本文介绍了在springmvc学习笔记系列的第十篇中,控制器的业务方法中如何通过注解实现封装Javabean来接收表单提交的数据。同时还讨论了当有多个注册表单且字段完全相同时,如何将其交给同一个控制器处理。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 本文介绍了Windows操作系统的版本及其特点,包括Windows 7系统的6个版本:Starter、Home Basic、Home Premium、Professional、Enterprise、Ultimate。Windows操作系统是微软公司研发的一套操作系统,具有人机操作性优异、支持的应用软件较多、对硬件支持良好等优点。Windows 7 Starter是功能最少的版本,缺乏Aero特效功能,没有64位支持,最初设计不能同时运行三个以上应用程序。 ... [详细]
  • 31.项目部署
    目录1一些概念1.1项目部署1.2WSGI1.3uWSGI1.4Nginx2安装环境与迁移项目2.1项目内容2.2项目配置2.2.1DEBUG2.2.2STAT ... [详细]
  • Spring常用注解(绝对经典),全靠这份Java知识点PDF大全
    本文介绍了Spring常用注解和注入bean的注解,包括@Bean、@Autowired、@Inject等,同时提供了一个Java知识点PDF大全的资源链接。其中详细介绍了ColorFactoryBean的使用,以及@Autowired和@Inject的区别和用法。此外,还提到了@Required属性的配置和使用。 ... [详细]
  • This article discusses the efficiency of using char str[] and char *str and whether there is any reason to prefer one over the other. It explains the difference between the two and provides an example to illustrate their usage. ... [详细]
  • Java 11相对于Java 8,OptaPlanner性能提升有多大?
    本文通过基准测试比较了Java 11和Java 8对OptaPlanner的性能提升。测试结果表明,在相同的硬件环境下,Java 11相对于Java 8在垃圾回收方面表现更好,从而提升了OptaPlanner的性能。 ... [详细]
  • 本文介绍了使用Python编写购物程序的实现步骤和代码示例。程序启动后,用户需要输入工资,并打印商品列表。用户可以根据商品编号选择购买商品,程序会检测余额是否充足,如果充足则直接扣款,否则提醒用户。用户可以随时退出程序,在退出时打印已购买商品的数量和余额。附带了完整的代码示例。 ... [详细]
author-avatar
手机用户2502877507
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有