使用dynamicNodeProvider的子节点

发布于 2024-12-22 21:13:33 字数 3338 浏览 1 评论 0原文

我使用 此项目 生成站点地图路径。

在我的应用程序中我希望站点地图路径保持如下:

路径: Home >项目>> {项目名称}
网址: /projects/{url-project}

路径: 首页 >项目>> {项目名称}>照片
网址: /projects/{url-project}/photos

路径: 首页 >项目>> {项目名称}>地址
网址: /projects/{url-project}/addresses

路径: 首页 >项目>> {项目名称}>管理员
Url: /projects/{url-project}/admin

尝试

为此,我创建了DynamicNodeProviderBase实现

public class ProjectDetailsDynamicNodeProvider : DynamicNodeProviderBase
{
    private readonly IProjectRepository _projectRepository;
    public ProjectDetailsDynamicNodeProvider()
    {
        _projectRepository = DependencyResolver.Current.GetService<IProjectRepository>();
    }

    #region Overrides of DynamicNodeProviderBase

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        // Build value 
        var returnValue = new List<DynamicNode>();
        var listDB = (from p in _projectRepository.Query()
                      select new { p.Name, p.Keyword, p.Description });

        // Create a node for each project
        foreach (var project in listDB.ToList())
        {
            var node = new DynamicNode { Title = project.Name, Description = project.Description };
            node.RouteValues.Add("url", project.Keyword);

            returnValue.Add(node);
        }

        // Return 
        return returnValue;
    }

    #endregion
}

< strong>Mvc.sitemap

<mvcSiteMapNode title="Projects" controller="Project" action="Index">
  <mvcSiteMapNode title="New project" controller="Project" action="Create" />

  <mvcSiteMapNode title="Project" action="About" controller="Project" dynamicNodeProvider="BindSolution.Infra.ProjectDetailsDynamicNodeProvider, BindSolution">
    <mvcSiteMapNode title="Photos" controller="Project" action="Photos" />
    <mvcSiteMapNode title="Addresses" controller="Project" action="Addresses" />
    <mvcSiteMapNode title="Admin" controller="Project" action="Admin" />
  </mvcSiteMapNode> 
</mvcSiteMapNode>

结果

路径: 首页 >项目>> {项目名称}
网址: /projects/{url-project}

路径: 项目 >照片
网址: /projects/{url-project}/photos

路径: 项目 >地址
网址: /projects/{url-project}/addresses

路径: 项目 >管理员
网址: /projects/{url-project}/admin

研究

在互联网上查找,我在 stackoverflow 上发现了这个问题,但不太明白解决方案或者它不适合我的问题。 无论如何,如果这是答案,您可以给我更多有关如何实施该解决方案的详细信息吗?

感谢大家的帮助!

I use this project to generate the sitemap path.

In my application I would like the sitemap path to stay as follows:

Path: Home > Projects > {project-name}
Url: /projects/{url-project}

Path: Home > Projects > {project-name} > Photos
Url: /projects/{url-project}/photos

Path: Home > Projects > {project-name} > Addresses
Url: /projects/{url-project}/addresses

Path: Home > Projects > {project-name} > Admin
Url: /projects/{url-project}/admin

Attemps

For this I created an implementation of DynamicNodeProviderBase

public class ProjectDetailsDynamicNodeProvider : DynamicNodeProviderBase
{
    private readonly IProjectRepository _projectRepository;
    public ProjectDetailsDynamicNodeProvider()
    {
        _projectRepository = DependencyResolver.Current.GetService<IProjectRepository>();
    }

    #region Overrides of DynamicNodeProviderBase

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        // Build value 
        var returnValue = new List<DynamicNode>();
        var listDB = (from p in _projectRepository.Query()
                      select new { p.Name, p.Keyword, p.Description });

        // Create a node for each project
        foreach (var project in listDB.ToList())
        {
            var node = new DynamicNode { Title = project.Name, Description = project.Description };
            node.RouteValues.Add("url", project.Keyword);

            returnValue.Add(node);
        }

        // Return 
        return returnValue;
    }

    #endregion
}

Mvc.sitemap

<mvcSiteMapNode title="Projects" controller="Project" action="Index">
  <mvcSiteMapNode title="New project" controller="Project" action="Create" />

  <mvcSiteMapNode title="Project" action="About" controller="Project" dynamicNodeProvider="BindSolution.Infra.ProjectDetailsDynamicNodeProvider, BindSolution">
    <mvcSiteMapNode title="Photos" controller="Project" action="Photos" />
    <mvcSiteMapNode title="Addresses" controller="Project" action="Addresses" />
    <mvcSiteMapNode title="Admin" controller="Project" action="Admin" />
  </mvcSiteMapNode> 
</mvcSiteMapNode>

Result

Path: Home > Projects > {project-name}
Url: /projects/{url-project}

Path: Project > Photos
Url: /projects/{url-project}/photos

Path: Project > Addresses
Url: /projects/{url-project}/addresses

Path: Project > Admin
Url: /projects/{url-project}/admin

Research

Looking on the internet, I found this question on stackoverflow, but not quite understand the resolution or it does not fit into my question.
Anyway, if this is the answer you could give me more details of how to implement the solution?

Thank you all for your help!

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

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

发布评论

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

评论(2

独闯女儿国 2024-12-29 21:13:33

我们的代码是/country/{countryname}/
/country/{countryname}/profiles

这就是我的想法。所以我会用 url-project 替换国家/地区。我注意到这种方法的唯一问题是我们的国家(我们有 210 个)内存不足,所以我不知道它是否是最好的。我在州(50)和其他一些州做了这件事,没有遇到问题。不过,我在国家/地区方面遇到了问题,而且我们在顶部列出了 9 个不同的页面。当我把它保持在4时,我没有问题。希望这有帮助。

public class CountriesDynamicNodeProvider : DynamicNodeProviderBase
{
    globalEDGEDataContext _db = new globalEDGEDataContext();
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        var returnValue = new List<DynamicNode>();
        foreach (var category in _db.CountriesList().Select(a => a.Title))
        {
            DynamicNode node = new DynamicNode("country_" + Tags.MakeUrlFriendly(category), category);
            node.RouteValues.Add("country", Tags.MakeUrlFriendly(category));
            yield return node;
        }
    }
}

public class CountriesPagesDynamicNodeProvider : DynamicNodeProviderBase
{
    globalEDGEDataContext _db = new globalEDGEDataContext();
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        foreach (var category in _db.CountriesList().Select(a=>a.Title))
        {
            DynamicNode node = new DynamicNode();
            node.Title = "Profile";
            node.ParentKey = "country_" + Tags.MakeUrlFriendly(category);
            node.RouteValues.Add("country", Tags.MakeUrlFriendly(category));
            yield return node;
        }
    }
}

该示例也适用于 MVC5。稍作修改:

  public class DepartmentEmployeeDynamicNodeProvider: DynamicNodeProviderBase
{
    AlertsContext db = new AlertsContext();

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
    {
        // Build value 
        var returnValue = new List<DynamicNode>();

        // Create a node for each department 
        foreach (var department in db.Departments)
        {
            var dynNode = new DynamicNode();
            dynNode.Title = department.DepartmentName;
            dynNode.RouteValues.Add("id", department.ID);

            returnValue.Add(dynNode);
        }

        // Return 
        return returnValue; 
    }
}

Our code is /country/{countryname}/
/country/{countryname}/profiles

And this is what I came up. So I would substitute country with url-project. The only problem that I am noticing with this method, so I don't know if it's the best, is that with our countries (we have 210) we run out of memory. I did this was states (50) and a few others and did not have a problem. I'm having a problem with countries though, also we have 9 different pages listed at the top. When I keep it to 4, I don't have a problem. Hope this helps.

public class CountriesDynamicNodeProvider : DynamicNodeProviderBase
{
    globalEDGEDataContext _db = new globalEDGEDataContext();
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        var returnValue = new List<DynamicNode>();
        foreach (var category in _db.CountriesList().Select(a => a.Title))
        {
            DynamicNode node = new DynamicNode("country_" + Tags.MakeUrlFriendly(category), category);
            node.RouteValues.Add("country", Tags.MakeUrlFriendly(category));
            yield return node;
        }
    }
}

public class CountriesPagesDynamicNodeProvider : DynamicNodeProviderBase
{
    globalEDGEDataContext _db = new globalEDGEDataContext();
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
    {
        foreach (var category in _db.CountriesList().Select(a=>a.Title))
        {
            DynamicNode node = new DynamicNode();
            node.Title = "Profile";
            node.ParentKey = "country_" + Tags.MakeUrlFriendly(category);
            node.RouteValues.Add("country", Tags.MakeUrlFriendly(category));
            yield return node;
        }
    }
}

The example works for MVC5 as well. Slightly modified:

  public class DepartmentEmployeeDynamicNodeProvider: DynamicNodeProviderBase
{
    AlertsContext db = new AlertsContext();

    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
    {
        // Build value 
        var returnValue = new List<DynamicNode>();

        // Create a node for each department 
        foreach (var department in db.Departments)
        {
            var dynNode = new DynamicNode();
            dynNode.Title = department.DepartmentName;
            dynNode.RouteValues.Add("id", department.ID);

            returnValue.Add(dynNode);
        }

        // Return 
        return returnValue; 
    }
}
允世 2024-12-29 21:13:33

对于要在父节点下注册的子节点,只需分配其父密钥即可。

For the children to be registered under a parent node, just assign their parent key.

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