CreatePageModel() public static method

Creates a Strongly Typed Page Model for a given DD4T Page and an optional set of include Pages.
public static CreatePageModel ( DD4T.ContentModel.IPage page, DD4T.ContentModel.IPage includes, Sdl.Web.Common.Configuration.Localization localization ) : PageModel
page DD4T.ContentModel.IPage The DD4T Page object.
includes DD4T.ContentModel.IPage The set of DD4T Page object for include pages. Can be null.
localization Sdl.Web.Common.Configuration.Localization The context .
return Sdl.Web.Common.Models.PageModel
Exemplo n.º 1
0
        protected virtual PageModel LoadPageModel(int pageId, bool addIncludes, Localization localization)
        {
            using (new Tracer(pageId, addIncludes, localization))
            {
                PageModelData pageModelData = SiteConfiguration.ModelServiceProvider.GetPageModelData(pageId, localization, addIncludes);

                if (pageModelData == null)
                {
                    throw new DxaItemNotFoundException($"Page not found for publication id {localization.Id} and page id {pageId}");
                }

                if (pageModelData.MvcData == null)
                {
                    throw new DxaException($"Data Model for Page '{pageModelData.Title}' ({pageModelData.Id}) contains no MVC data. Ensure that the Page is published using the DXA R2 TBBs.");
                }

                return(ModelBuilderPipeline.CreatePageModel(pageModelData, addIncludes, localization));
            }
        }
        private PageModel LoadPageModel(ref string urlPath, bool addIncludes, Localization localization)
        {
            using (new Tracer(urlPath, addIncludes, localization))
            {
                PageModelData pageModelData = SiteConfiguration.ModelServiceProvider.GetPageModelData(urlPath, localization, addIncludes);

                if (pageModelData == null)
                {
                    throw new DxaItemNotFoundException(urlPath);
                }

                if (pageModelData.MvcData == null)
                {
                    throw new DxaException($"Data Model for Page '{pageModelData.Title}' ({pageModelData.Id}) contains no MVC data. Ensure that the Page is published using the DXA R2 TBBs.");
                }

                return(ModelBuilderPipeline.CreatePageModel(pageModelData, addIncludes, localization));
            }
        }
Exemplo n.º 3
0
#pragma warning restore 618


        /// <summary>
        /// Gets a Page Model for a given URL.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <param name="localization">The context Localization.</param>
        /// <param name="addIncludes">Indicates whether include Pages should be expanded.</param>
        /// <returns>The Page Model.</returns>
        /// <exception cref="DxaItemNotFoundException">If no Page Model exists for the given URL.</exception>
        public virtual PageModel GetPageModel(string url, Localization localization, bool addIncludes)
        {
            using (new Tracer(url, localization, addIncludes))
            {
                //We can have a couple of tries to get the page model if there is no file extension on the url request, but it does not end in a slash:
                //1. Try adding the default extension, so /news becomes /news.html
                IPage page = GetPage(url, localization);
                if (page == null && (url == null || (!url.EndsWith("/") && url.LastIndexOf(".", StringComparison.Ordinal) <= url.LastIndexOf("/", StringComparison.Ordinal))))
                {
                    //2. Try adding the default page, so /news becomes /news/index.html
                    page = GetPage(url + "/", localization);
                }
                if (page == null)
                {
                    throw new DxaItemNotFoundException(url);
                }
                FullyLoadDynamicComponentPresentations(page, localization);

                IPage[] includes = addIncludes ? GetIncludesFromModel(page, localization).ToArray() : new IPage[0];

                return(ModelBuilderPipeline.CreatePageModel(page, includes, localization));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets a Page Model for a given URL.
        /// </summary>
        /// <param name="urlPath">The URL path (unescaped).</param>
        /// <param name="localization">The context Localization.</param>
        /// <param name="addIncludes">Indicates whether include Pages should be expanded.</param>
        /// <returns>The Page Model.</returns>
        /// <exception cref="DxaItemNotFoundException">If no Page Model exists for the given URL.</exception>
        public virtual PageModel GetPageModel(string urlPath, Localization localization, bool addIncludes)
        {
            using (new Tracer(urlPath, localization, addIncludes))
            {
                if (urlPath == null)
                {
                    urlPath = "/";
                }
                else if (!urlPath.StartsWith("/"))
                {
                    urlPath = "/" + urlPath;
                }

                IPage page = GetPage(urlPath, localization);
                if (page == null && !urlPath.EndsWith("/"))
                {
                    // This may be a SG URL path; try if the index page exists.
                    urlPath += Constants.IndexPageUrlSuffix;
                    page     = GetPage(urlPath, localization);
                }
                else if (urlPath.EndsWith("/"))
                {
                    urlPath += Constants.DefaultExtensionLessPageName;
                }

                if (page == null)
                {
                    throw new DxaItemNotFoundException(urlPath, localization.Id);
                }

                IPage[] includes = addIncludes ? GetIncludesFromModel(page, localization).ToArray() : new IPage[0];

                List <string> dependencies = new List <string>()
                {
                    page.Id
                };
                dependencies.AddRange(includes.Select(p => p.Id));

                PageModel result = null;
                if (CacheRegions.IsViewModelCachingEnabled)
                {
                    PageModel cachedPageModel = SiteConfiguration.CacheProvider.GetOrAdd(
                        string.Format("{0}:{1}", page.Id, addIncludes), // Cache Page Models with and without includes separately
                        CacheRegions.PageModel,
                        () =>
                    {
                        PageModel pageModel = ModelBuilderPipeline.CreatePageModel(page, includes, localization);
                        pageModel.Url       = urlPath;
                        if (pageModel.NoCache)
                        {
                            result = pageModel;
                            return(null);
                        }
                        return(pageModel);
                    },
                        dependencies
                        );

                    if (cachedPageModel != null)
                    {
                        // Don't return the cached Page Model itself, because we don't want dynamic logic to modify the cached state.
                        result = (PageModel)cachedPageModel.DeepCopy();
                    }
                }
                else
                {
                    result     = ModelBuilderPipeline.CreatePageModel(page, includes, localization);
                    result.Url = urlPath;
                }

                if (SiteConfiguration.ConditionalEntityEvaluator != null)
                {
                    result.FilterConditionalEntities(localization);
                }

                return(result);
            }
        }