MVCSiteMapProvider自定义MvcRouteHandler没有面包屑

时间:2011-09-08 04:13:57

标签: breadcrumbs sitemapprovider sitemappath

我在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个动作索引发送到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()中,但页面显示正确。我的代码中有什么问题? 所以我可以用breadcrumbs字符串构建自定义页面的树...

1 个答案:

答案 0 :(得分:1)

请提供您的Mvc.sitemap。

您的DynamicNode实例似乎缺少ParentKey。