静态页生成、敏感词过滤

静态页生成

首先先写一个html模板,里面会改变的地方设置为参数(用于替换)

        /// <summary>
        /// 根据书的编号生成一个html静态页
        /// </summary>
        /// <param name="book"></param>
        public void CreateHtml(Model.Books book)
        {
            //读取模板页的html
            string html = File.ReadAllText(HttpContext.Current.Request.MapPath("/CreateHtmlPage/HtmlContent.htm"));
            //替换
            html = html.Replace("$Title", book.Title).Replace("$Author", book.Author).Replace("$content", book.ContentDescription).Replace("$Price", book.UnitPrice.ToString()).Replace("$Path", "/Images/BookCovers/"+book.ISBN+".jpg").Replace("$bookid",book.Id.ToString());
            //设置生成页的路径
            string path = "/CreateHtmlPage/html/" + book.PublishDate.Year + "/" + book.PublishDate.Month + "/" + book.PublishDate.Day + "/";
            //创建该路径
            Directory.CreateDirectory(HttpContext.Current.Request.MapPath(path));          
            path = path + book.Id + ".html";
            //保存到设置路径下
            File.WriteAllText(HttpContext.Current.Request.MapPath(path), html,System.Text.Encoding.Default);
        }

        //根据id,获得相应id对应商品的详情页
        public string Getpath(int id)
        {
            BLL.Books bookbll = new BLL.Books();
            Model.Books book = bookbll.GetModel(id);
            return "/CreateHtmlPage/html/" + book.PublishDate.Year + "/" + book.PublishDate.Month + "/" + book.PublishDate.Day + "/"+id+".html";
        }

敏感词过滤

首先建立一个敏感词表

包含字段:Id wordPattrrn IsForbid  IsMod ReplaceWord

把词库导入数据库

首先写一个类,类里面包含

Model

public class Article_Words
    {
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string wordPattern;

        public string WordPattern
        {
            get { return wordPattern; }
            set { wordPattern = value; }
        }
        private bool isForbid;

        public bool IsForbid
        {
            get { return isForbid; }
            set { isForbid = value; }
        }
        private bool isMod;

        public bool IsMod
        {
            get { return isMod; }
            set { isMod = value; }
        }
        private string replaceWord;

        public string ReplaceWord
        {
            get { return replaceWord; }
            set { replaceWord = value; }
        }
    }

 

DAL

View Code
public int Add(BookShop.Model.Articel_Words model)
        {
            StringBuilder strSql=new StringBuilder();
            strSql.Append("insert into Articel_Words(");
            strSql.Append("WordPattern,IsForbid,IsMod,ReplaceWord)");
            strSql.Append(" values (");
            strSql.Append("@WordPattern,@IsForbid,@IsMod,@ReplaceWord)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@WordPattern", SqlDbType.NVarChar,50),
                    new SqlParameter("@IsForbid", SqlDbType.Bit,1),
                    new SqlParameter("@IsMod", SqlDbType.Bit,1),
                    new SqlParameter("@ReplaceWord", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.WordPattern;
            parameters[1].Value = model.IsForbid;
            parameters[2].Value = model.IsMod;
            parameters[3].Value = model.ReplaceWord;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

UI层:

View Code
   protected void Button1_Click(object sender, EventArgs e)
        {
            string str = Request["text"];
            //根据换行分割
            string[] strs = str.Split(new char[]{'\r','\n'},StringSplitOptions.RemoveEmptyEntries);
            BLL.Articel_Words wordbll = new BLL.Articel_Words();

            foreach (var item in strs)
            {
                Model.Articel_Words articel = new Model.Articel_Words();
                string [] codes=item.Split('=');
                articel.WordPattern = codes[0];
                //是否是禁用词
                if (codes[1]=="{BANNED}")
                {
                    articel.IsForbid = true;
                }
                //是否是审查词
                else if (codes[1]=="{MOD}")
                {
                    articel.IsMod = true;
                }
                //替换词
                else
                {
                    articel.ReplaceWord = codes[1];
                }
                wordbll.Add(articel);
            }
        }

 

 

posted @ 2013-05-03 15:45  卡西法儿  阅读(839)  评论(2编辑  收藏  举报