private List <InvisibleContentProjection> CollectInvisibleRegionsRecursively(
            RenderPageViewModel renderingPageModel,
            IEnumerable <PageContentProjection> contentProjections,
            List <InvisibleContentProjection> invisibleContentProjections,
            InvisibleContentProjection parentContentProjection = null)
        {
            // Add all invisible contents to the invisible list
            foreach (var projection in contentProjections)
            {
                var invisibleProjection = new InvisibleContentProjection
                {
                    Parent            = parentContentProjection,
                    ContentProjection = projection
                };
                invisibleContentProjections.Add(invisibleProjection);

                if (!renderingPageModel.RenderedPageContents.Contains(projection.PageContentId) || (parentContentProjection != null && parentContentProjection.IsInvisible))
                {
                    invisibleProjection.IsInvisible = true;
                }

                var childContentRegionProjections = projection.GetChildRegionContentProjections();
                if (childContentRegionProjections != null && childContentRegionProjections.Any())
                {
                    invisibleContentProjections = CollectInvisibleRegionsRecursively(renderingPageModel, childContentRegionProjections.Distinct(), invisibleContentProjections, invisibleProjection);
                }
            }

            return(invisibleContentProjections);
        }
        private List<InvisibleContentProjection> CollectInvisibleRegionsRecursively(
            RenderPageViewModel renderingPageModel,
            IEnumerable<PageContentProjection> contentProjections,
            List<InvisibleContentProjection> invisibleContentProjections,
            InvisibleContentProjection parentContentProjection = null)
        {
            // Add all invisible contents to the invisible list
            foreach (var projection in contentProjections)
            {
                var invisibleProjection = new InvisibleContentProjection
                {
                    Parent = parentContentProjection,
                    ContentProjection = projection
                };
                invisibleContentProjections.Add(invisibleProjection);

                if (!renderingPageModel.RenderedPageContents.Contains(projection.PageContentId) || (parentContentProjection != null && parentContentProjection.IsInvisible))
                {
                    invisibleProjection.IsInvisible = true;
                }

                var childContentRegionProjections = projection.GetChildRegionContentProjections();
                if (childContentRegionProjections != null && childContentRegionProjections.Any())
                {
                    invisibleContentProjections = CollectInvisibleRegionsRecursively(renderingPageModel, childContentRegionProjections.Distinct(), invisibleContentProjections, invisibleProjection);
                }
            }

            return invisibleContentProjections;
        }
        private StringBuilder RenderInvisibleRegionsRecursively(StringBuilder contentsBuilder,
            IEnumerable<InvisibleContentProjection> invisibleContentProjections, InvisibleContentProjection parent = null)
        {
            foreach (var group in invisibleContentProjections.Where(i => i.Parent == parent).GroupBy(i => i.ContentProjection.RegionId))
            {
                var regionModel = new PageRegionViewModel { RegionId = group.Key, RegionIdentifier = group.First().ContentProjection.RegionIdentifier };
                using (new LayoutRegionWrapper(contentsBuilder, regionModel, true, true))
                {
                    foreach (var projection in group)
                    {
                        using (new RegionContentWrapper(contentsBuilder, projection.ContentProjection, true, true))
                        {
                            RenderInvisibleRegionsRecursively(contentsBuilder, invisibleContentProjections, projection);
                        }
                    }
                }

            }

            return contentsBuilder;
        }
        private StringBuilder RenderInvisibleRegionsRecursively(StringBuilder contentsBuilder,
                                                                IEnumerable <InvisibleContentProjection> invisibleContentProjections, InvisibleContentProjection parent = null)
        {
            foreach (var group in invisibleContentProjections.Where(i => i.Parent == parent).GroupBy(i => i.ContentProjection.RegionId))
            {
                var regionModel = new PageRegionViewModel {
                    RegionId = group.Key, RegionIdentifier = group.First().ContentProjection.RegionIdentifier
                };
                using (new LayoutRegionWrapper(contentsBuilder, regionModel, true, true))
                {
                    foreach (var projection in group)
                    {
                        using (new RegionContentWrapper(contentsBuilder, projection.ContentProjection, true, true))
                        {
                            RenderInvisibleRegionsRecursively(contentsBuilder, invisibleContentProjections, projection);
                        }
                    }
                }
            }

            return(contentsBuilder);
        }