使用dynamicNodeProvider的子节点
我使用 此项目 生成站点地图路径。
在我的应用程序中我希望站点地图路径保持如下:
路径: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们的代码是/country/{countryname}/
/country/{countryname}/profiles
这就是我的想法。所以我会用 url-project 替换国家/地区。我注意到这种方法的唯一问题是我们的国家(我们有 210 个)内存不足,所以我不知道它是否是最好的。我在州(50)和其他一些州做了这件事,没有遇到问题。不过,我在国家/地区方面遇到了问题,而且我们在顶部列出了 9 个不同的页面。当我把它保持在4时,我没有问题。希望这有帮助。
该示例也适用于 MVC5。稍作修改:
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.
The example works for MVC5 as well. Slightly modified:
对于要在父节点下注册的子节点,只需分配其父密钥即可。
For the children to be registered under a parent node, just assign their parent key.