Episerver 检查内容区域是否为空

Episerver check if content area is empty

我需要检查内容区域是否为空,但出现错误 "Object referrence not set to an instance",这是我的页面控制器,我也试过 currentPage.TabContentArea.IsEmpty,仍然是同样的错误。内容区域是空的,这是我第一次尝试 运行 它所以我需要在执行 if 语句中的代码之前检查它是否为空。

     public class StandardPageController : PageController<StandardPage>
    {
        // GET: StandardPage
        public   ActionResult Index( StandardPage currentPage)
        {

            // this collection should be used in foreach loops
            var tabItems = new List<TabViewModel>();


//this is where I get error
            if(currentPage.TabContentArea.FilteredItems.Any())

 { 
            var contentAreaItems = currentPage.TabContentArea.FilteredItems.ToList();
            var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();

            foreach (var contentAreaItem in contentAreaItems)
            {
                // get an instance of Tab Block
                // If you didn't set any restrictions, ContentArea can contain anything.
                // We need to check if blockData is of type PageTab
                var blockData = contentLoader.Get<PageTab>(contentAreaItem.ContentLink);
                if (blockData == null) continue;

                tabItems.Add(new TabViewModel
                {

                    Id = Guid.NewGuid(),
                    Title = blockData.TabTitle,
                    Text = blockData.TabContent
                });
            }
            ViewBag.items = tabItems;
            }
            return View(); // Should I return tabitems here ?
        }
    }

ContentArea 属性 可以为空,因此您需要先检查 currentPage.TabContentArea 是否为空。

if(currentPage.TabContentArea != null && currentPage.TabContentArea.FilteredItems.Any()) { ... }