MVCSiteMapProvider 自定义 MvcRouteHandler 没有面包屑
我在 global.asax 中有 2 条路由
routes.MapRoute(
"DefaultFriendlyUrl",
"Page/{FriendlyUrl}",
null,
new string[] { "MvcApplication2.Controllers" }
).RouteHandler = new FriendlyUrlRouteHandler();
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "index", id = UrlParameter.Optional },
new string[] { "MvcApplication2.Controllers" }
);
,因此,FriendlyUrlRouteHandler 可以处理我的所有 /Page/blablabla 路由,并使用 1 个操作 Index 发送到 PageController
public class FriendlyUrlRouteHandler : MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var friendlyUrl = (string)requestContext.RouteData.Values["FriendlyUrl"];
PageItem page = null;
if (!string.IsNullOrEmpty(friendlyUrl))
page = PageManager.GetPageByFriendlyUrl(friendlyUrl);
if (page == null)
{
requestContext.RouteData.Values["controller"] = "home";
requestContext.RouteData.Values["action"] = "index";
requestContext.RouteData.Values["id"] = null;
}
else
{
requestContext.RouteData.Values["controller"] = "page";
requestContext.RouteData.Values["action"] = "index";
requestContext.RouteData.Values["id"] = page.PageID;
}
return base.GetHttpHandler(requestContext);
}
}
,然后 PageController 获取我的页面内容并显示它。但 MvcSiteMapProvider 不显示这些页面的面包屑
SiteMap.cs
public class SiteMap : DynamicNodeProviderBase
{
public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
{
var returnValue = new List<DynamicNode>();
returnValue.Add(new DynamicNode() { Key = "id1", Title="CustomPage", Controller="Page", Action="Index" });
return returnValue;
}
}
我的 CustomPage 不存在于 @Html.MvcSiteMap().SiteMapPath() 中,但页面显示正确。我的代码有什么问题吗? 所以我无法在面包屑字符串中构建自定义页面的树......
I have 2 routes in global.asax
routes.MapRoute(
"DefaultFriendlyUrl",
"Page/{FriendlyUrl}",
null,
new string[] { "MvcApplication2.Controllers" }
).RouteHandler = new FriendlyUrlRouteHandler();
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "index", id = UrlParameter.Optional },
new string[] { "MvcApplication2.Controllers" }
);
so, FriendlyUrlRouteHandler work all my /Page/blablabla routes and send to PageController with 1 action Index
public class FriendlyUrlRouteHandler : MvcRouteHandler
{
protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var friendlyUrl = (string)requestContext.RouteData.Values["FriendlyUrl"];
PageItem page = null;
if (!string.IsNullOrEmpty(friendlyUrl))
page = PageManager.GetPageByFriendlyUrl(friendlyUrl);
if (page == null)
{
requestContext.RouteData.Values["controller"] = "home";
requestContext.RouteData.Values["action"] = "index";
requestContext.RouteData.Values["id"] = null;
}
else
{
requestContext.RouteData.Values["controller"] = "page";
requestContext.RouteData.Values["action"] = "index";
requestContext.RouteData.Values["id"] = page.PageID;
}
return base.GetHttpHandler(requestContext);
}
}
Then PageController get content for my page and show it. But MvcSiteMapProvider don't show breadcrumbs for these pages
SiteMap.cs
public class SiteMap : DynamicNodeProviderBase
{
public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
{
var returnValue = new List<DynamicNode>();
returnValue.Add(new DynamicNode() { Key = "id1", Title="CustomPage", Controller="Page", Action="Index" });
return returnValue;
}
}
And my CustomPage doesn,t exists in @Html.MvcSiteMap().SiteMapPath(), but page is showed correctly. What,s wrong in my code?
So I can,t build tree of my custom pages in breadcrumbs string...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请提供您的 Mvc.sitemap。
您的 DynamicNode 实例似乎缺少 ParentKey。
Please provide your Mvc.sitemap.
Your instance of DynamicNode appears to be missing the ParentKey.