在 ActionResult 中创建/删除站点地图节点 - ASP.NET MVC SiteMap 提供程序

发布于 2024-09-16 02:16:53 字数 3727 浏览 4 评论 0原文

我正在使用 ASP.NET MVC SiteMap 提供程序 创建动态站点地图节点。它在应用程序首次加载时工作,但我希望在 ActionResult 执行时添加/删除节点(从数据库中添加/删除记录)。我的 web.sitemap 如下(取出父节点和其他同级节点)

<mvcSiteMapNode dynamicNodeProvider="MyWebSite.DynamicNodeProvider.DatesDynamicNodeProvider, MyWebSite">
    <mvcSiteMapNode action="Details" dynamicNodeProvider="MyWebSite.DynamicNodeProvider.DetailsDynamicNodeProvider, MyWebSite" />
    <mvcSiteMapNode action="Edit" dynamicNodeProvider="MyWebSite.DynamicNodeProvider.EditDynamicNodeProvider, MyWebSite" />
    <mvcSiteMapNode action="Delete" dynamicNodeProvider="MyWebSite.DynamicNodeProvider.DeleteDynamicNodeProvider, MyWebSite" />
</mvcSiteMapNode>

日期是动态生成的:

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        DBDataContext db = new DBDataContext();

        var records = db.records.ToList();
        if (records != null)
        {
            DateTime created;
            foreach (var record in records)
            {
                if (record.DateCreated.HasValue)
                {
                    created = record.DateCreated.Value;
                    DynamicNode node = CreateNode(created);
                    yield return node;
                }

            }
        }
    }

    public static DynamicNode CreateNode(DateTime date)
    {
        DynamicNode node = new DynamicNode("record_" + date.ToString("yyyy-M"), date.ToString("MMMM yyyy"));
        node.Action = date.ToString("yyyy-M");
        node.ParentKey = "records";
        return node;
    }

然后在这些生成的节点下添加节点:

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        DBDataContext db = new DBDataContext();

        var records = db.records.ToList();
        if (records != null)
        {
            // Create a node for each record
            foreach (var record in records)
            {
                if (record.DateCreated.HasValue)
                {
                    DynamicNode node = CreateNode(record);

                    yield return node;
                }
            }
        }
    }

    public static DynamicNode CreateNode(Record record)
    {
        DateTime created = record.DateCreated.Value;
        DynamicNode node = new DynamicNode("record_edit_" + record.ID, string.Format("{0:dd MMM yyyy}: {1} (Edit)", created, record.Title));
        node.RouteValues["id"] = record.ID;
        node.ParentKey = "record_" + created.ToString("yyyy-M");

        return node;
    }

但是,然后我开始在 ActionResult 中创建这些节点,但是得到卡在这一点上。我可以抓取该节点:

var node = ((MvcSiteMapProvider.DefaultSiteMapProvider)SiteMap.Provider).FindSiteMapNodeFromKey("record_edit_" + record.ID);

但是我想检查该节点是否为空,如果不是,则创建一个新节点(如果不存在,则创建父节点)。我尝试的第一件事不起作用,因为不确定是否可以将 DynamicNode 添加到站点地图(因为我无法将 DynamicNode 转换为 SiteMapNode)

    if (node == null)
    {
        node = DynamicNodeProvider.EditDynamicNodeProvider.CreateNode(record) as SiteMapNode;
        var parent = ((MvcSiteMapProvider.DefaultSiteMapProvider)SiteMap.Provider).FindSiteMapNodeFromKey("record_" + record.DateCreated.Value.ToString("yyyy-M"));
        if (parent == null)
        {
            parent = DynamicNodeProvider.DatesDynamicNodeProvider.CreateNode(record.DateCreated.Value) as SiteMapNode;
            parent.Action = record.DateCreated.Value.ToString("yyyy-M");
            parent.ParentKey = "records";
        }
        node.ParentKey = parent.ParentKey;
    }

当然,由于上述问题,这不起作用。

这仅适用于每条记录的编辑页面,我还想更改标题(如果标题已更改)或在必要时从站点地图中删除。

有什么想法吗?

I am using the ASP.NET MVC SiteMap provider to create dynamic sitemap nodes. It works when the application is first loaded, but I wish to add/remove nodes when an ActionResult executes (to add/remove a record from the database). My web.sitemap is as follows (taken out the parent node and other siblings)

<mvcSiteMapNode dynamicNodeProvider="MyWebSite.DynamicNodeProvider.DatesDynamicNodeProvider, MyWebSite">
    <mvcSiteMapNode action="Details" dynamicNodeProvider="MyWebSite.DynamicNodeProvider.DetailsDynamicNodeProvider, MyWebSite" />
    <mvcSiteMapNode action="Edit" dynamicNodeProvider="MyWebSite.DynamicNodeProvider.EditDynamicNodeProvider, MyWebSite" />
    <mvcSiteMapNode action="Delete" dynamicNodeProvider="MyWebSite.DynamicNodeProvider.DeleteDynamicNodeProvider, MyWebSite" />
</mvcSiteMapNode>

The dates are generated dynamically:

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        DBDataContext db = new DBDataContext();

        var records = db.records.ToList();
        if (records != null)
        {
            DateTime created;
            foreach (var record in records)
            {
                if (record.DateCreated.HasValue)
                {
                    created = record.DateCreated.Value;
                    DynamicNode node = CreateNode(created);
                    yield return node;
                }

            }
        }
    }

    public static DynamicNode CreateNode(DateTime date)
    {
        DynamicNode node = new DynamicNode("record_" + date.ToString("yyyy-M"), date.ToString("MMMM yyyy"));
        node.Action = date.ToString("yyyy-M");
        node.ParentKey = "records";
        return node;
    }

And then add nodes under these generated nodes:

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        DBDataContext db = new DBDataContext();

        var records = db.records.ToList();
        if (records != null)
        {
            // Create a node for each record
            foreach (var record in records)
            {
                if (record.DateCreated.HasValue)
                {
                    DynamicNode node = CreateNode(record);

                    yield return node;
                }
            }
        }
    }

    public static DynamicNode CreateNode(Record record)
    {
        DateTime created = record.DateCreated.Value;
        DynamicNode node = new DynamicNode("record_edit_" + record.ID, string.Format("{0:dd MMM yyyy}: {1} (Edit)", created, record.Title));
        node.RouteValues["id"] = record.ID;
        node.ParentKey = "record_" + created.ToString("yyyy-M");

        return node;
    }

However, I then get to the point of creating these nodes within an ActionResult, but get stuck at this point. I can grab the node:

var node = ((MvcSiteMapProvider.DefaultSiteMapProvider)SiteMap.Provider).FindSiteMapNodeFromKey("record_edit_" + record.ID);

But then I want to check if the node is null, and if not create a new one (and the parent node if it does not exist). First thing I tried doesn't work, as not sure if you can add a DynamicNode to the site map (as I can't cast DynamicNode to SiteMapNode)

    if (node == null)
    {
        node = DynamicNodeProvider.EditDynamicNodeProvider.CreateNode(record) as SiteMapNode;
        var parent = ((MvcSiteMapProvider.DefaultSiteMapProvider)SiteMap.Provider).FindSiteMapNodeFromKey("record_" + record.DateCreated.Value.ToString("yyyy-M"));
        if (parent == null)
        {
            parent = DynamicNodeProvider.DatesDynamicNodeProvider.CreateNode(record.DateCreated.Value) as SiteMapNode;
            parent.Action = record.DateCreated.Value.ToString("yyyy-M");
            parent.ParentKey = "records";
        }
        node.ParentKey = parent.ParentKey;
    }

Of course, this doesn't work because of the aforementioned problem.

This is just for the edit page for each record, I also want change the title (if the title is changed) or delete from the sitemap if necessary.

Any ideas?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独难免 2024-09-23 02:16:53

尝试设置对动态节点提供程序的缓存依赖项,并让它们在需要时(例如添加记录时)触发站点地图更新。

Try setting the cache dependency on your dynamic node providers and have them trigger a sitemap update when needed (for example when a record is added).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文