gpt4 book ai didi

mapping - 将 Web.SiteMap 与动态 URL(URL 路由)一起使用

转载 作者:行者123 更新时间:2023-12-04 03:47:40 26 4
gpt4 key购买 nike

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

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

因此,如果我必须为页面上的 10,000 篇文章中的每一篇都拥有一个站点地图节点,如下所示:

  • 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

  • 我可以使用 SiteMap 进行某种通配符映射以匹配文章下的任何内容吗?

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

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

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

    最佳答案

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

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

    <siteMapNode url="/article.aspx" title="(this will be replaced)" param="id" />

    然后,您创建这个类:
    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
    <write your own code>

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

    最后,您可以在代码中使用此提供程序,就像使用静态提供程序一样。
    <mycontrols:DynamicSiteMapPath ID="dsmpMain" runat="server" />

    我的课比这更复杂,但这些是基础。您可以不使用查询字符串参数,而是分析您正在使用的友好 url,并使用它来检索正确的内容。为了最大限度地减少每个请求的额外数据库查找,您可以向提供者添加缓存机制(文章标题通常不会经常更改)。

    希望这可以帮助。

    关于mapping - 将 Web.SiteMap 与动态 URL(URL 路由)一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/291908/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com