/// <summary>
 /// Adds a page to CacheList and Cache[CACHE_KEY]
 /// </summary>
 /// <param name="page"></param>
 /// <param name="param"></param>
 /// <param name="param2"></param>
 public void AddPage(Page page, string param = null, string param2 = null)
 {
     if (Cache != null && this.AllowCache)
     {
         var queryString = HttpContext.Current.Request.QueryString.ToDictionary();
         var pageCache   = new PageCacheModel(page, param, param2, queryString);
         this.UpdateCacheList();
         this.CacheList.Add(pageCache);
         Cache[CACHE_KEY] = this.CacheList;
     }
 }
示例#2
0
        /// <summary>
        /// Cast a PageCacheModel to a Page type
        /// </summary>
        /// <param name="this"></param>
        /// <returns></returns>
        public static Page ToPage(this PageCacheModel @this)
        {
            var page = new Page
            {
                Name             = @this.Name,
                Section          = @this.Section,
                CompiledModel    = @this.CompiledModel,
                CompiledTemplate = @this.CompiledTemplate,
                Model            = @this.Model,
                Template         = @this.Template,
                HasParams        = @this.HasParams
            };

            return(page);
        }
        /// <summary>
        /// Searches CacheList and returns a PageCacheModel of the page if found
        /// </summary>
        /// <param name="name"></param>
        /// <param name="section"></param>
        /// <param name="param"></param>
        /// <param name="param2"></param>
        /// <param name="queryString"></param>
        /// <returns></returns>
        public PageCacheModel FindPage(string name, string section, string param = null, string param2 = null, IDictionary <string, string> queryString = null)
        {
            if (this.CacheList == null)
            {
                this.UpdateCacheList();
            }
            var pageCache = new PageCacheModel();

            if (this.CacheList.Count > 0)
            {
                pageCache = this.CacheList.FirstOrDefault(
                    cachedPage => string.Equals(cachedPage.Name, name, StringComparison.InvariantCultureIgnoreCase) &&
                    string.Equals(cachedPage.Section, section, StringComparison.InvariantCultureIgnoreCase) &&
                    string.Equals(cachedPage.Param, param, StringComparison.InvariantCultureIgnoreCase) &&
                    string.Equals(cachedPage.Param2, param2, StringComparison.InvariantCultureIgnoreCase) &&
                    cachedPage.QueryStringParams.Equal(queryString));
            }

            return(pageCache);
        }
        /// <summary>
        /// Returns view with the template for the passed in name and variable. The
        /// PageTemplate model is passed to the view.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="section"></param>
        /// <param name="param"></param>
        /// <param name="param2"></param>
        /// <returns></returns>
        // GET: /Section/Name
        public ActionResult View(string section, string name, string param = null, string param2 = null)
        {
            Page page = null; // will store the page once we find it
            Func <Page, bool> templateNeedsCompiled = iPage => (string.IsNullOrEmpty(iPage.CompiledTemplate) &&
                                                                (!string.IsNullOrEmpty(iPage.CompiledModel)) &&
                                                                !string.IsNullOrEmpty(iPage.Template)) ||
                                                      iPage.HasInclude;
            // templage model that will be passed to the View
            var template = new PageTemplate {
                Content = string.Empty
            };

            // if AllowCache enabled in Web.Config look for the page in cache
            if (this.AllowCache)
            {
                PageCacheModel cachedPage = this.CacheManager.FindPage(name, section, param, param2, this.QueryStringParams);
                if (cachedPage != null && cachedPage.CompiledTemplate != null)
                {
                    page = cachedPage.ToPage();
                }
            }

            // if we couldn't get the page from cache get it from the db
            if (page == null || page.CompiledTemplate == null)
            {
                page = this._repository.FindPage(section, name);
                if (page.HasParams)
                {
                    page.CompiledTemplate = null;
                }
            }

            // when page doesn't have parameters can use pre-compiled template
            var templateIsCompiled = page != null && !templateNeedsCompiled(page) && !page.HasParams;

            if (templateIsCompiled)
            {
                template.Content = page.CompiledTemplate ?? page.Template;
            }
            else if (page != null && (page.HasParams || templateNeedsCompiled(page)))
            {
                // if page has url params pass them to model and compile it
                if (page.HasParams || string.IsNullOrEmpty(page.CompiledModel))
                {
                    using (var stringCompiler = new StringCompiler())
                    {
                        stringCompiler.CompilePageModel(page.Model, param, param2);
                        if (stringCompiler.IsValid)
                        {
                            page.CompiledModel = stringCompiler.ToString();
                        }
                        else
                        {
                            this.Errors.AddRange(stringCompiler.Errors);
                        }
                    } // end using stringCompiler
                }     // end if page.HasParams and page / param are not null

                if (templateNeedsCompiled(page))
                {
                    ConcurrentBag <string> errors = this.Errors;
                    page.CompileTemplate(ref errors, page.Template, page.CompiledModel);
                }

                template.Content = page.CompiledTemplate;
            } // end else if page has paramaters

            // cache page before returning template if enabled
            if (this.AllowCache)
            {
                if (page != null && !this.CacheManager.PageCacheExists(page.Name, page.Section, param, param2, this.QueryStringParams))
                {
                    this.CacheManager.AddPage(page, param, param2);
                }
            }

            // return the page with a template if it is found
            if (!string.IsNullOrEmpty(template.Content))
            {
                return(View(template));
            }


            if (Errors.Count > 0)
            {
                return(View("~/Views/ServerError.cshtml", Errors));
            }

            // return 404 view if could not find page in db or files
            return(View("~/Views/NotFound.cshtml"));
        }