Exemplo n.º 1
0
 /// <summary>
 /// 呈现标签页
 /// </summary>
 /// <param name="context"></param>
 /// <param name="t"></param>
 public static void RenderTag(CmsContext context, string t)
 {
     //检测网站状态
     if (!context.CheckSiteState())
     {
         return;
     }
     try
     {
         ICmsPageGenerator cmsPage = new PageGeneratorObject(context);
         var html = cmsPage.GetTagArchive(t ?? string.Empty);
         context.HttpContext.Response.WriteAsync(html);
     }
     catch (TemplateException ex)
     {
         RenderError(context, ex, false);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 呈现首页
        /// </summary>
        /// <param name="context"></param>
        public static void RenderIndex(CmsContext context)
        {
            try
            {
                var site    = context.CurrentSite;
                var siteId  = site.SiteId;
                var cacheId = $"cms_s{siteId}_{(int) Cms.Context.DeviceType}_index_page";
                ICmsPageGenerator cmsPage = new PageGeneratorObject(context);
                if (context.HttpContext.Request.Query("cache") == "0")
                {
                    Cms.Cache.Rebuilt();
                }
                string html;
                if (Settings.Opti_IndexCacheSeconds > 0)
                {
                    ICmsCache cache = Cms.Cache;
                    var       obj   = cache.Get(cacheId);
                    if (obj == null)
                    {
                        html = cmsPage.GetIndex();
                        cache.Insert(cacheId, html, DateTime.Now.AddSeconds(Settings.Opti_IndexCacheSeconds));
                    }
                    else
                    {
                        html = obj as string;
                    }
                }
                else
                {
                    html = cmsPage.GetIndex();
                }

                context.HttpContext.Response.WriteAsync(html);
            }
            catch (TemplateException ex)
            {
                RenderError(context, ex, false);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 呈现搜索页
        /// </summary>
        /// <param name="context"></param>
        /// <param name="c"></param>
        /// <param name="w"></param>
        public static void RenderSearch(CmsContext context, string c, string w)
        {
            //检测网站状态
            if (!context.CheckSiteState())
            {
                return;
            }

            ICmsPageGenerator cmsPage = new PageGeneratorObject(context);

            try
            {
                context.HttpContext.Response.WriteAsync(
                    cmsPage.GetSearch(
                        c ?? string.Empty
                        , w ?? string.Empty
                        )
                    );
            }
            catch (TemplateException ex)
            {
                RenderError(context, ex, false);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 呈现分类页
        /// </summary>
        /// <param name="context"></param>
        /// <param name="catPath"></param>
        /// <param name="page"></param>
        public static void RenderCategory(CmsContext context, string catPath, int page)
        {
            if (catPath.StartsWith("/"))
            {
                catPath = catPath.Substring(1);
            }
            // 去掉末尾的"/"
            if (catPath.EndsWith("/"))
            {
                catPath = catPath.Substring(0, catPath.Length - 1);
            }
            var siteId   = context.CurrentSite.SiteId;
            var category = ServiceCall.Instance.SiteService.GetCategory(siteId, catPath);

            if (!(category.ID > 0))
            {
                RenderNotFound(context);
                return;
            }

            if (!catPath.StartsWith(category.Path))
            {
                RenderNotFound(context);
                return;
            }

            ICmsPageGenerator cmsPage = new PageGeneratorObject(context);

            /*********************************
            *  @ 单页,跳到第一个特殊文档,
            *  @ 如果未设置则最新创建的文档,
            *  @ 如未添加文档则返回404
            *********************************/
            if (string.IsNullOrEmpty(category.Location))
            {
                try
                {
                    var html = cmsPage.GetCategory(category, page);
                    context.HttpContext.Response.WriteAsync(html);
                }
                catch (TemplateException ex)
                {
                    RenderError(context, ex, false);
                }
            }
            else
            {
                var url = category.Location;
                if (category.Location.IndexOf("://") != -1)
                {
                    url = category.Location;
                }
                else
                {
                    if (!category.Location.StartsWith("/"))
                    {
                        url = string.Concat(context.SiteAppPath, context.SiteAppPath == "/" ? "" : "/",
                                            category.Location);
                    }
                }

                context.HttpContext.Response.Redirect(url, false); //302
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// 访问文档
        /// </summary>
        /// <param name="context"></param>
        /// <param name="archivePath">文档路径</param>
        public static void RenderArchive(CmsContext context, string archivePath)
        {
            if (archivePath.StartsWith("/"))
            {
                archivePath = archivePath.Substring(1);
            }
            var siteId  = context.CurrentSite.SiteId;
            var archive = ServiceCall.Instance.ArchiveService.GetArchiveByIdOrAlias(siteId, archivePath);

            if (archive.Id <= 0)
            {
                RenderNotFound(context);
                return;
            }

            // 如果设置了302跳转
            if (!string.IsNullOrEmpty(archive.Location))
            {
                string url;
                if (Regex.IsMatch(archive.Location, "^http://", RegexOptions.IgnoreCase))
                {
                    url = archive.Location;
                }
                else
                {
                    if (archive.Location.StartsWith("/"))
                    {
                        throw new Exception("URL不能以\"/\"开头!");
                    }
                    url = string.Concat(context.SiteDomain, "/", archive.Location);
                }

                context.HttpContext.Response.Redirect(url, false); //302
                return;
            }

            ICmsPageGenerator cmsPage = new PageGeneratorObject(context);

            if (!FlagAnd(archive.Flag, BuiltInArchiveFlags.Visible))
            {
                RenderNotFound(context);
                return;
            }

            var category = archive.Category;

            if (!(category.ID > 0))
            {
                RenderNotFound(context);
                return;
            }

            if (FlagAnd(archive.Flag, BuiltInArchiveFlags.AsPage))
            {
                var pagePattern = "^[\\.0-9A-Za-z_-]+\\.html$";

                if (!Regex.IsMatch(context.HttpContext.Request.GetPath(), pagePattern))
                {
                    context.HttpContext.Response.StatusCode(301);
                    context.HttpContext.Response.AppendHeader("Location",
                                                              $"{(string.IsNullOrEmpty(archive.Alias) ? archive.StrId : archive.Alias)}.html");
                    return;
                }
            }
            else
            {
                //校验栏目是否正确
                var categoryPath = category.Path;
                if (!archivePath.StartsWith(categoryPath + "/"))
                {
                    RenderNotFound(context);
                    return;
                }
            }

            //增加浏览次数
            ++archive.ViewCount;
            ServiceCall.Instance.ArchiveService.AddCountForArchive(siteId, archive.Id, 1);
            try
            {
                //显示页面
                var html = cmsPage.GetArchive(archive);
                context.HttpContext.Response.WriteAsync(html);
            }
            catch (TemplateException ex)
            {
                RenderError(context, ex, false);
            }
        }