生成静态页

原理:利用uedit编辑页面,content内容保存到数据库中,content插入到模板中

1、读取页面

/// <summary>
    /// 生成其他类别新闻静态页面
    /// </summary>
    /// <param name="newsModel"></param>
    /// <returns></returns>
    private string CreateOtherTypeNewsStaticPage(Bitauto.Life.Model.News newsModel)
    {
        string strPagePath = "";
        string tempPath = "";
        if (newsModel.FirstCId == 11)
        {
            tempPath = System.Web.HttpContext.Current.Server.MapPath("../templetPage/templetGuidlinePage_View.htm");
        }
        else
        {
            tempPath = System.Web.HttpContext.Current.Server.MapPath("../templetPage/templetOtherTypeNews_View.htm");
        }
        string relativePath = System.Configuration.ConfigurationManager.AppSettings["StaticPagePath"].ToString();
        string savePath = System.Web.HttpContext.Current.Server.MapPath("../" + relativePath);
        if (!System.IO.Directory.Exists(savePath))//保存路径如果不存在,则创建新路径
        {
            System.IO.Directory.CreateDirectory(savePath);
        }
        string deptName = Bitauto.Life.BLL.News.Instance.GetUserDept(newsModel.CreateUserId);
        if (Bitauto.Life.BLL.PublicStaticNewsHelper.Instance.ProcessNewStaticNewsHtm(newsModel, tempPath, savePath, deptName, out strPagePath, newsModel.FirstCId == 11 ? (newsModel.SecondCId == 16 ? "NewGuidline_Android.htm" : "NewGuidline_IOS.htm") : ""))
        {
            return System.Configuration.ConfigurationManager.AppSettings["BaseIPUrl"].ToString() + relativePath + strPagePath;
        }
        else
        {
            return "";
        }
    }

2、替换关键字

/// <summary>
        /// 生成静态页面
        /// </summary>
        /// <param name="model">新闻实体</param>
        /// <param name="templetPath">模板路径</param>
        /// <param name="savePath">生成的静态页的存储路径</param>
        /// <param name="saveFileName">静态页访问的url</param>
        /// <param name="staticPageName">指定生成文件的名称(如果需要生成固定名称的静态页文件,请传入文件名称,如果不是固定名称的,请传入空字符串)</param>
        /// <returns></returns>
        public bool ProcessNewStaticNewsHtm(Bitauto.Life.Model.News model, string templetPath, string savePath,string deptName,out string saveFileName,string staticPageName = "")
        {
            bool falg = true;
            StreamReader sr = null;
            saveFileName = "";
            try
            {
                sr = new StreamReader(templetPath);
                string msgs = sr.ReadToEnd();
                msgs = msgs.Replace("{$NewsID}", model.NewsID.ToString());
                msgs = msgs.Replace("{$Title}", model.Title);
                msgs = msgs.Replace("{$Digest}", model.Digest);

                string secondCIdName = "";
                DataTable dt = DAL.NewsCategory.Instance.GetList(new Bitauto.Life.Model.NewsCategory() { CID = model.SecondCId });
                if (dt.Rows.Count > 0)
                {
                    secondCIdName = dt.Rows[0]["Name"] as string;
                }

                msgs = msgs.Replace("{$SecondCIDName}", secondCIdName);
                msgs = msgs.Replace("{$StaticPageUrl}", model.StaticPageUrl);
                msgs = msgs.Replace("{$PublishTime}", model.PublishTime.Value.ToString("yyyy.MM.dd"));

                string newContent = model.Content; //RegReplaceContent(model.Content);
                msgs = msgs.Replace("{$Content}", newContent);

                msgs = msgs.Replace("{$DeptName}", deptName);

                Random r = new Random();
                if (staticPageName != "")
                {
                    saveFileName = staticPageName;
                }
                else
                {
                    saveFileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + r.Next(10) + ".htm";
                }
                File.WriteAllText(savePath + saveFileName, msgs);
            }
            catch (Exception ex)
            {
                falg = false;
                Util.Log("Log", "error", "生成静态页面出错,错误信息为:" + ex.Message);
            }
            finally
            {
                sr.Close();
            }
            return falg;
        }

 

posted @ 2017-01-24 16:01  wjl910  阅读(49)  评论(0)    收藏  举报