问题描述
我正在为我的ASP.NET MVC 3项目使用MVCSitemapProvider,我在新闻稿部分有一个动态节点.我面临的问题是一个部分有多个节点,当我查看站点地图时,它可以正常工作:
I'm using the MVCSitemapProvider for my ASP.NET MVC 3 project, I have a dynamic node for a press release section. The issue I'm facing is that there are multiple nodes for a section and when I look at the site map it's working correctly as so:
> Home
> Static Node - Press Releases
>Dynamic node - Press Release #1
>Dynamic node - Press Release #2
>Dynamic node - Press Release #3
但是,在面包屑中,如果我导航到Press Release#2页面.面包屑将显示第一个节点.
However in the bread crumbs, if I navigate to the Press Release #2 page. the Breadcrumb will show the first node.
Home > Press Releases > Press Release #1
这是节点提供者的代码
public class PressReleaseDynamicNodeProvider : DynamicNodeProviderBase {
public PressReleasesRepository _repository = new PressReleasesRepository();
public List<PressRelease> releases = new List<PressRelease>();
public override IEnumerable<DynamicNode> GetDynamicNodeCollection() {
releases = _repository.GetPressReleases();
var returnValue = new List<DynamicNode>();
foreach (var release in releases) {
DynamicNode node = new DynamicNode();
node.Title = release.Title;
returnValue.Add(node);
}
return returnValue;
}
}
和MVC站点地图代码:
and the MVC sitemap code:
<mvcSiteMapNode controller="News" action="PressReleases" title="Press Releases" >
<mvcSiteMapNode controller="News" action="PressRelease" title="" dynamicNodeProvider="MySite.NodeProvider.PressReleaseDynamicNodeProvider, MySite" />
</mvcSiteMapNode>
在我重写此问题时,我一直在研究代码,没有进行任何重大更改,现在...新闻稿#1显示新闻稿#1,新闻稿#2显示新闻稿# 3和新闻发布#3显示新闻发布#1.
As I was rewriting this quesiton, I've been playing around with the code, not making any major changes and NOW... Press Release #1 is showing Press Release #1, Press Release #2 is showing Press Release #3 and Press Release #3 is showing Press Release #1.
我对此感到困惑.
推荐答案
原因是站点地图提供程序无法区分您的路线.现在,所有新闻稿都通过相同的URL/news/pressreleases/访问.为了使面包屑正确无误,您应该通过node.RouteValues
将类似id的内容传递给提供程序.
The reason for this is that the sitemap provider can't distinguish between your routes. Right now all press releases are accessed through the same URL, /news/pressreleases/. To make the breadcrumbs correct you should pass something like an id, to the provider through node.RouteValues
.
例如,您在NewsController
中的操作方法PressReleases
可能看起来像这样:
For example, your action method PressReleases
in NewsController
may look something like this:
public ActionResult PressReleases(int id) {
... implementation ...
}
然后您将像这样将id
参数传递到RouteValues
中:
Then you would pass the id
parameter into the RouteValues
like this:
foreach (var release in releases) {
DynamicNode node = new DynamicNode();
node.Title = release.Title;
node.RouteValues.Add("id", release.Id);
returnValue.Add(node);
}
这个小小的变化将使面包屑按预期工作.
This small change will make the breadcrumbs work as expected.
这篇关于MVCSiteMapProvider动态节点始终返回面包屑中所有页面的第一个节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!