它不起作用的原因是您没有考虑所有路由值,即您有一个名为slug
您需要配置节点来匹配。
如果您希望节点匹配而不管 的值如何slug
(即使它是空白的),你应该使用PreservedRouteParameters
来匹配它。否则,您应该将其添加到RouteValues
并且该节点只会匹配one您为其配置的值(如果需要,您可以添加其他节点以匹配其他值)。我正在展示PreservedRouteParameters
接近这里。
此外,您还可以通过配置来有效禁用 MVC 支持Url
动态节点上的属性。如果您需要使用非 MVC 页面或外部 URL,此属性非常有用,但不建议用于 MVC。
MvcSiteMapProvider
直接取决于 MVC 路由配置。您可以在此处配置 URL,使其呈现您想要的外观。对于您期望的 URL (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;
}
}
}