EpiServer 9 mvc 添加页面到导航

EpiServer 9 mvc add page to Navigation

我正在玩 Episerver 网站。我没有使用模板,只是一个空白的 Episerver 站点。

我创建了一个页面类型 Ex。以编程方式启动页面,我可以将其作为新页面添加到编辑视图中。所以我的问题是如何让我创建的新页面显示在我的导航栏中?谢谢!

   <div class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav">
                <!-- Some code here?? -->

            </ul>
        </div>
    </div>
</div>

查看 Alloy 模板,这是您之前从未使用过 Episerver 的最佳入门方式:https://github.com/episerver/AlloyDemoKit

这是我在旧项目中解决的方法:

  1. 创建一个名为 "Helpers"

  2. 的新文件夹
  3. 新建 class "NavigationHelper":

    using EPiServer;
    using EPiServer.Core;
    using EPiServer.Filters;
    using EPiServer.ServiceLocation;
    using EPiServer.Web.Mvc.Html;
    using EPiServer.Web.Routing;
    using System.Linq;
    using System.Web.Mvc;
    
    namespace Demo.Helpers
    {
        public static class NavigationHelper
        {
        public static void RenderMainNavigation(this HtmlHelper html, PageReference rootLink = null, ContentReference contentLink = null, bool includeRootPage = true, IContentLoader contentLoader = null)
        {
            var writer = html.ViewContext.Writer;
    
            contentLink = contentLink ?? @html.ViewContext.RequestContext.GetContentLink();
            rootLink = rootLink ?? ContentReference.StartPage;
    
            if (includeRootPage)
            {
                if (rootLink.CompareToIgnoreWorkID(contentLink))
                {
                    writer.WriteLine("<li class=\"active\">");
                }
                else
                {
                    writer.WriteLine("<li>");
                }
                writer.WriteLine(html.ContentLink(rootLink).ToHtmlString());
                writer.WriteLine("</li>");
            }
    
            contentLoader = contentLoader ?? ServiceLocator.Current.GetInstance<IContentLoader>();
    
            var topLevelPages = contentLoader.GetChildren<PageData>(ContentReference.StartPage).ToList();
            topLevelPages = FilterForVisitor.Filter(topLevelPages).OfType<PageData>().Where(x => x.VisibleInMenu).ToList();
    
            var currentBranch = contentLoader.GetAncestors(contentLink).Select(x => x.ContentLink).ToList();
    
            currentBranch.Add(contentLink);
    
            foreach (var topLevelPage in topLevelPages)
            {
                if (currentBranch.Any(x => x.CompareToIgnoreWorkID(topLevelPage.ContentLink)))
                {
                    writer.WriteLine("<li class=\"active\">");
                }
                else
                {
                    writer.WriteLine("<li>");
                }
                writer.WriteLine(html.PageLink(topLevelPage).ToHtmlString());
                writer.WriteLine("</li>");
            }
        }
    }
    }
    

该代码有点粗糙,但它可以工作,并且会为您提供一些有关如何根据您的需要对其进行自定义的信息。

  1. 在布局页面的顶部为新 class 添加一个 using。例如:

    @using Demo.Helpers;
    
  2. 在您的布局中将“”替换为:

    @{
      Html.RenderMainNavigation();
     }
    

现在您应该有一个可以为您的项目自定义的工作菜单:)

我在旧网站做的项目中找到了这段比较简单的代码。代码可能取自 Joel Abrahamsson 所著的 "EPiServer 7 CMS Development" 一书。或者它可能来自 Alloy 模板,但如果我没记错的话 Alloy 中的代码对于主菜单来说有点复杂。