修改:这是我的课程
public class MyDynamicNodeProvider
: DynamicNodeProviderBase
{
public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
{
webdata storeDB = new webdata();
var returnValue = new List<DynamicNode>();
foreach (var article in storeDB.SiteContents)
{
DynamicNode enode = new DynamicNode();
enode.Title = article.ArticleTitle;
enode.ParentKey = "ArticleID";
enode.Url = "ArticleDetails/" + article.ArticleID + "/" + article.ArticleAlias;
//Specify Controller and Action name
enode.Controller = "SiteContents";
enode.Action = "ArticleDetails";
enode.RouteValues.Add("id", article.ArticleID);
returnValue.Add(enode);
yield return enode;
}
}
}
修改:这是我的站点地图文件
<mvcSiteMapNode title="Home" controller="Home" action="Index">
<mvcSiteMapNode title="About Us" controller="Menu" action="AboutUs">
<mvcSiteMapNode title="Profile" controller="Menu" action="Profile"/>
<mvcSiteMapNode title="History" controller="Menu" action="History"/>
</mvcSiteMapNode>
<mvcSiteMapNode title="Article" controller="SiteContents" action="ArticleDetails" key="ArticleID">
<mvcSiteMapNode title="Details" dynamicNodeProvider="Myproject.Models.MyDynamicNodeProvider, Myproject" />
</mvcSiteMapNode>
编辑:我拥有的第二个控制器(SiteContentsController)
public ActionResult ArticleDetails(int? id, string slug)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SiteContents siteContents = db.SiteContents.Find(id);
if (siteContents == null)
{
return HttpNotFound();
}
if (string.IsNullOrWhiteSpace(slug))
{
var alias = db.SiteContents.First(p => p.ArticleID == id).ArticleAlias;
return RedirectToAction("ArticleDetails", new { id = id, slug = alias });
}
return View(siteContents);
}
我想要的网址(哪个有效,但它没有带来网站地图
是http://localhost:xxxx/ArticleDetails/1/Quality_Policy
我在布局页面上调用了站点地图
@Html.MvcSiteMap().SiteMapPath()
修改:我的route.config
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(name: "Articles", url: "ArticleDetails/{id}/{slug}", defaults: new { controller = "SiteContents", action = "ArticleDetails", id = UrlParameter.Optional, slug = UrlParameter.Optional });
routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {controller = "Home", action = "Index", id = UrlParameter.Optional});
}
我也有一些静态节点可以正常工作。问题是在动态页面中没有返回任何内容,我也没有收到任何错误消息 谢谢
答案 0 :(得分:1)
它不起作用的原因是您没有考虑所有路由值,即您有一个名为slug
的路由值,您需要将该节点配置为匹配。
如果您希望节点匹配,而不管slug
的值是什么(即使它是空白的),您应该使用PreservedRouteParameters
来匹配它。否则,您应该将其添加到RouteValues
,并且该节点将仅匹配您为其配置的 one 值(如果需要,您可以添加其他节点以匹配其他值)。我在这里展示PreservedRouteParameters
方法。
此外,通过在动态节点上配置Url
属性,您已经有效地禁用了MVC支持。如果您需要使用非MVC页面或外部URL,则此属性非常有用,但不建议用于MVC。
MvcSiteMapProvider
直接取决于MVC路由配置。您可以在此处配置网址,以查看您希望它们的外观。要使您的预期网址(http://localhost:xxxx/ArticleDetails/1/Quality_Policy
)生效,您需要一个匹配此模式的相应路由,如下所示。
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Route to match the URL /ArticleDetails/1/Quality_Policy
routes.MapRoute(
name: "ArticleDetails",
url: "ArticleDetails/{id}/{slug}",
defaults: new { controller = "SiteContents", action = "ArticleDetails", slug = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
您遇到的另一个问题是您要将动态节点附加到的节点。您当前配置节点的方式是ArticleDetails
操作。我无法告诉你在这里要做什么。通常,您将显示所有文章页面的列表(索引),然后当用户单击文章时,您将显示它。这是一个例子。
// NOTE: Normally, you would put all of your Article stuff
// into an ArticleController
public class SiteContentsController
{
// NOTE: Normally, this would be named ArticleController.Index()
public ActionResult ArticleIndex()
{
// NOTE: You may want to use a view model here
// rather than using the SiteContents directly.
var siteContents = db.SiteContents.ToList();
return View(siteContents);
}
// NOTE: Normally, this would be named ArticleController.Details()
public ActionResult ArticleDetails(int? id, string slug)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
SiteContents siteContents = db.SiteContents.Find(id);
if (siteContents == null)
{
return HttpNotFound();
}
if (string.IsNullOrWhiteSpace(slug))
{
var alias = db.SiteContents.First(p => p.ArticleID == id).ArticleAlias;
return RedirectToAction("ArticleDetails", new { id = id, slug = alias });
}
return View(siteContents);
}
}
您的Mvc.sitemap
文件看起来更像这样(文章位于主页下方)。我相信这是您的主要问题 - 您的XML文件中只能有一个根节点(通常是网站的主页)。
<mvcSiteMapNode title="Home" controller="Home" action="Index">
<mvcSiteMapNode title="About Us" controller="Menu" action="AboutUs">
<mvcSiteMapNode title="Profile" controller="Menu" action="Profile">
<mvcSiteMapNode title="Quality Policy" controller="Menu" action="Policy"/>
</mvcSiteMapNode>
<mvcSiteMapNode title="History" controller="Menu" action="History"/>
<mvcSiteMapNode title="Articles" controller="SiteContents" action="ArticleIndex" key="Articles">
<mvcSiteMapNode title="Details" dynamicNodeProvider="Myproject.Models.MyDynamicNodeProvider, Myproject" />
</mvcSiteMapNode>
</mvcSiteMapNode>
最后,我们更正了DynamicNodeProvider
。
public class MyDynamicNodeProvider
: DynamicNodeProviderBase
{
public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)
{
webdata storeDB = new webdata();
foreach (var article in storeDB.SiteContents)
{
DynamicNode enode = new DynamicNode();
enode.Title = article.ArticleTitle;
enode.ParentKey = "Articles";
// Don't use the Url property unless you have a
// non-MVC page/external URL
//enode.Url = "ArticleDetails/" + article.ArticleID + "/" + article.ArticleAlias;
// Specify Controller, Action name, and id.
// These values all must match the request in order
// for the node to be considered the "current" node
enode.Controller = "SiteContents";
enode.Action = "ArticleDetails";
enode.RouteValues.Add("id", article.ArticleID);
// Match the slug (we don't really care what its value is here)
enode.PreservedRouteParameters.Add("slug");
yield return enode;
}
}
}