MVCSiteMapProvider 自定义 MvcRouteHandler 没有面包屑

发布于 2024-12-03 12:08:19 字数 2188 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2024-12-10 12:08:19

请提供您的 Mvc.sitemap。

您的 DynamicNode 实例似乎缺少 ParentKey。

Please provide your Mvc.sitemap.

Your instance of DynamicNode appears to be missing the ParentKey.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文