private ContentContext(ContentContext parent, ContentExplorer contentExplorer)
 {
     Page       = parent.Page;
     Services   = parent.Services;
     Explorer   = contentExplorer;
     IsDesigner = parent.IsDesigner;
 }
        protected override async Task OnPageRequestAsync(PageRequestContext context)
        {
            PageService = HttpContext.RequestServices.GetRequiredService <IPageService>();

            if (Request.Query.TryGetValue("editId", out string editIdValue))
            {
                if (!Guid.TryParse(editIdValue, out Guid editId))
                {
                    context.Result = BadRequest();
                    return;
                }

                var pageEditingService = HttpContext.RequestServices.GetRequiredService <IPageContentService>();
                editSession = await pageEditingService.FindEditByIdAsync(editId);

                if (editSession == null)
                {
                    context.Result = NotFound();
                    return;
                }

                page = await PageService.FindPageByIdAsync(editSession.PageId);

                if (page == null)
                {
                    context.Result = NotFound();
                    return;
                }

                var accessProvider = HttpContext.RequestServices.GetRequiredService <Identity.IAccessProvider>();

                if (!await accessProvider.CheckAccessAsync() || await accessProvider.GetUserIdAsync() != editSession.UserId)
                {
                    var pageLinkGenerator = HttpContext.RequestServices.GetRequiredService <IPageLinkGenerator>();

                    context.Result = RedirectPermanent(await pageLinkGenerator.GetPathAsync(page));
                    return;
                }
            }
            else
            {
                var routeData = RouteData;

                var pagePath = string.Empty;
                if (routeData.Values.TryGetValue("url", out object urlValue) && urlValue != null)
                {
                    pagePath = (string)urlValue;
                }

                var url = await PageService.FindPageUrlAsync(pagePath);

                if (url == null)
                {
                    context.Result = NotFound();
                    return;
                }

                if (url.PageId.HasValue)
                {
                    page = await PageService.FindPageByIdAsync(url.PageId.Value);

                    if (page == null)
                    {
                        context.Result = NotFound();
                        return;
                    }

                    if (!page.IsPublished)
                    {
                        var accessProvider = HttpContext.RequestServices.GetRequiredService <Identity.IAccessProvider>();
                        if (!await accessProvider.CheckAccessAsync())
                        {
                            context.Result = NotFound();
                            return;
                        }
                    }
                }
                else
                {
                    var pageLinkGenerator = HttpContext.RequestServices.GetRequiredService <IPageLinkGenerator>();
                    var redirectUrl       = await pageLinkGenerator.GetPathAsync(url.Redirect.Path);

                    if (url.Redirect.IsPermament)
                    {
                        context.Result = RedirectPermanent(redirectUrl);
                    }
                    else
                    {
                        context.Result = Redirect(redirectUrl);
                    }
                    return;
                }
            }

            PageMetadata = await PageService.GetPageTypeAsync(page, HttpContext.RequestAborted);

            pageSeo = await PageService.GetPageSeoOptionsAsync(page, HttpContext.RequestAborted);

            if (editSession != null)
            {
                var pageEditingService = HttpContext.RequestServices.GetRequiredService <IPageContentService>();
                PageContent = await pageEditingService.GetContentAsync(editSession, HttpContext.RequestAborted);
            }
            else
            {
                PageContent = await PageService.GetPageContentAsync(page, HttpContext.RequestAborted);
            }
            if (PageContent == null)
            {
                throw new InvalidOperationException();
            }

            ContentContext = new ContentContext(page, PageContent, HttpContext.RequestServices, editSession != null);

            Status       = page.IsPublished ? Models.PageStatus.Published : Models.PageStatus.Draft;
            ParentPageId = await PageService.GetParentPageIdAsync(page, HttpContext.RequestAborted);
        }