3

我使用这个项目来生成站点地图路径。

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

路径: Home > Projects > {project-name}
网址: /projects/{url-project}

路径: Home > Projects > {project-name} > Photos
网址: /projects/{url-project}/photos

路径: Home > Projects > {project-name} > Addresses
网址: /projects/{url-project}/addresses

路径: Home > Projects > {project-name} > Admin
网址: /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
}

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>

结果

路径: Home > Projects > {project-name}
网址: /projects/{url-project}

路径: Project > Photos
网址: /projects/{url-project}/photos

路径: Project > Addresses
网址: /projects/{url-project}/addresses

路径: Project > Admin
网址: /projects/{url-project}/admin

研究

在网上看,我在 stackoverflow 上找到了这个问题,但不太了解分辨率,或者它不适合我的问题。无论如何,如果这是答案,你可以给我更多关于如何实施解决方案的细节?

感谢大家的帮助!

4

2 回答 2

1

我们的代码是 /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; 
    }
}
于 2012-03-05T12:50:15.037 回答
0

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

于 2012-03-19T14:27:44.073 回答