我使用这个项目来生成站点地图路径。
在我的应用程序中,我希望站点地图路径保持如下:
路径: 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 上找到了这个问题,但不太了解分辨率,或者它不适合我的问题。无论如何,如果这是答案,你可以给我更多关于如何实施解决方案的细节?
感谢大家的帮助!