.net 生成静态页面

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.IO;

using System.Text;

 

/// <summary>

/// ModelDemo 静态生成

/// </summary>

///

namespace Maticsoft.DBUtility

{

    public class ModelDemo

    {

        accessDate sql = new accessDate();

        /// <summary>

        /// 生成文件夹

        /// </summary>

        /// <returns></returns>

        public string ModelFile()

        {

            string str = DateTime.Now.ToString("yyyy-MM");

            if (!Directory.Exists(HttpContext.Current.Server.MapPath("/" + str)))

            {

                Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/" + str));

            }

            return str;

        }

 

        /// <summary>

        /// 读取Html文件

        /// </summary>

        /// <param name="readAddress">Html相对地址</param>

        /// <returns></returns>

        public string readHtml(string readAddress)

        {

            try

            {

                using (StreamReader sr = new StreamReader(HttpContext.Current.Server.MapPath(readAddress)))

                {

                    string line = "";

                    line = sr.ReadToEnd();

                    return line;

                }

 

            }

            catch (Exception ex)

            {

                throw new Exception(ex.ToString());

            }

        }

        /// <summary>

        /// 自由静态生成

        /// </summary>

        /// <param name="strModel">输入的页面字符串信息</param>

        /// <param name="Parth">需要输出前台显示的路径,如(about.html)</param>

        public void CreateCompany(string strModel, string Parth)

        {

            Encoding code = Encoding.GetEncoding("utf-8");

            StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath("/" + Parth), false, code);

            sw.Write(strModel);

            sw.Flush();

            sw.Close();

        }

        /// <summary>

        /// 静态分页列表

        /// </summary>

        /// <param name="i">当前页数</param>

        /// <param name="Indexcount">总页数</param>

        /// <param name="strName">名称(about/news/product)</param>

        /// <returns></returns>

        public string getList(int i, int Indexcount, string strName)

        {

            //public string returnPage(int i, int Indexcount,string strName)

 

            string strContent = "";

            strContent += "<a href=\"" + strName + ".html#now\">首页</a>";

 

            if (Indexcount == 1)

            {

                strContent += "<a href=\"javascript:void(0)\">上一页</a>";

                strContent += "<a href=\"javascript:void(0)\">下一页</a>";

                strContent += "<a href=\"" + strName + ".html#now\">尾页</a>";

            }

            else

            {

                int up = i - 1;

                if (up == 0 || up == 1)

                {

                    strContent += "<a href=\"" + strName + ".html#now\">上一页</a>";

                }

                else

                {

                    strContent += "<a href=\"" + strName + "_" + up + ".html#now\">上一页</a>";

                }

                int down = i + 1;

                if (down > Indexcount)

                {

                    strContent += "<a href=\"javascript:void(0)\">下一页</a>";

                }

                else

                {

                    strContent += "<a href=\"" + strName + "_" + down + ".html#now\">下一页</a>";

                }

                strContent += "<a href=\"" + strName + "_" + Indexcount + ".html#now\">尾页</a>";

            }

 

            strContent += dropList(i, Indexcount, strName);

            return strContent;

 

        }

 

 

        /// <summary>

        /// 生成新闻的详细信息

        /// </summary>

        /// <param name="ReplaceChar">替换字符串的数组</param>

        /// <param name="ReplaceContent">替换的内容更跟替换字符串的数组一致</param>

        /// <param name="Parth">模板路劲,相对路径</param>

        /// <param name="_Adress">添加修改文件,添加时为空</param>

        /// <returns>输入输出路径</returns>

        public string AddInfo(string[] ReplaceChar, string[] ReplaceContent, string Parth, string _Adress)

        {

            string strModel = readHtml(Parth);

            //=====

            string strpath = DateTime.Now.ToString("yyyy_MM_dd_hh_mmss") + ".html";

            string htmlfilename = ModelFile() + "/" + strpath;

            if (_Adress != "")

            {

                htmlfilename = _Adress;//不为空时,覆盖原来的文件

            }

            //=====

            Encoding code = Encoding.GetEncoding("utf-8");

            StreamWriter sw = null;

            for (int i = 0; i < ReplaceChar.Length; i++)

            {

                strModel = strModel.Replace(ReplaceChar[i], ReplaceContent[i]);

            }

            //strModel = strModel.Replace("$title$", title);

            //strModel = strModel.Replace("$titleright$", titleright);

            //strModel = strModel.Replace("$Content$", Content);

 

            sw = new StreamWriter(HttpContext.Current.Server.MapPath("/" + htmlfilename), false, code);

            sw.Write(strModel);

            sw.Flush();

            sw.Close();

            return htmlfilename;

        }

 

  

        /// <summary>

        /// 生成新闻下一页上一页

        /// </summary>

        /// <param name="i">当前的页码</param>

        /// <param name="Indexcount">总页数</param>

        /// <param name="count">总条数</param>

        /// <returns></returns>

        public string returnPage(int i, int Indexcount, string strName)

        {

            string strContent = "";

            strContent += "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\" style=\"width: 508px\">";

            strContent += "<tr>";

            strContent += "<td valign=\"middle\" style=\"width: 86px\">";

            strContent += "<a href='" + strName + ".html'>首页</a>";

            strContent += "</td>";

            strContent += "<td valign=\"middle\" style=\"width: 86px\">";

 

            if (Indexcount == 1)

            {

                strContent += "<a href='javascript:void(0)'>上一页</a>";

                strContent += "</td>";

                strContent += "<td valign=\"middle\" style=\"width: 86px\">";

                strContent += "<a href='javascript:void(0)'>下一页</a>";

                strContent += "</td>";

                strContent += "<td valign=\"middle\" style=\"width: 73px\">";

                strContent += "<a href='" + strName + ".html'>尾页</a>";

            }

            else

            {

                int up = i - 1;

                if (up == 0 || up == 1)

                {

                    strContent += "<a href='" + strName + ".html'>上一页</a>";

                }

                else

                {

                    strContent += "<a href='" + strName + "_" + up + ".html'>上一页</a>";

                }

                strContent += "</td>";

                strContent += "<td valign=\"middle\" style=\"width: 86px\">";

                int down = i + 1;

                if (down > Indexcount)

                {

                    strContent += "<a href='javascript:void(0)'>下一页</a>";

                }

                else

                {

                    strContent += "<a href='" + strName + "_" + down + ".html'>下一页</a>";

                }

                strContent += "</td>";

                strContent += "<td valign=\"middle\" style=\"width: 73px\">";

                strContent += "<a href='" + strName + "_" + Indexcount + ".html'>尾页</a>";

            }

 

            strContent += "</td>";

            strContent += "<td valign=\"top\" style=\"width: 100px; font-size: 12px; padding-bottom: 8px;\">";

            strContent += dropList(i, Indexcount, strName);

            strContent += "</tr>";

            strContent += " </table>";

            return strContent;

        }

 

        /// <summary>

        /// 生静下拉列表

        /// </summary>

        /// <param name="i">当前的页码</param>

        /// <param name="Indexcount">总页数</param>

        /// <param name="strName">生静页面名称</param>

        /// <returns></returns>

        public string dropList(int i, int Indexcount, string strName)

        {

            string strContent = "";

            if (Indexcount == 1)

            {

                strContent += "<select οnchange=\"location.href=this.value\"><option value='" + strName + ".html#now' selected=\"selected\">1</option></select>";

            }

            else

            {

                strContent += "<select οnchange=\"location.href=this.value\">";

                for (int j = 1; j <= Indexcount; j++)

                {

                    if (i == j && i == 1 && j == 1)

                    {

                        strContent += "<option value=\"" + strName + ".html#now\" selected=\"selected\">" + 1 + "</option>";

                    }

                    else if (i == j && i != 1 && j != 1)

                    {

                        strContent += "<option value=\"" + strName + "_" + j + ".html#now\" selected=\"selected\">" + j + "</option>";

                    }

                    else

                    {

                        if (j == 1)

                        {

                            strContent += "<option value=\"" + strName + ".html#now\">1</option>";

                        }

                        else

                        {

                            strContent += "<option value=\"" + strName + "_" + j + ".html#now\">" + j + "</option>";

                        }

                    }

                }

                strContent += "</select>";

            }

            return strContent;

        }

 

 

 

        public string returnDivlink()

        {

            DataTable dt = sql.GetTable("select * from link");

            string strContent = "";

            foreach (DataRow dr in dt.Rows)

            {

                strContent += "<a href='" + dr["linkaddress"].ToString() + "' target=\"_blank\">" + dr["linkname"].ToString() + "</a>";

            }

            return strContent;

        }

        /// <summary>

        /// 模板转换

        /// </summary>

        public void AllModelList(string title, string content, string strpath, string table)

        {

            DataTable dt = sql.GetTable("select * from Model");

            DataTable dt1 = sql.GetTable(string.Format("select * from {0}", table));

            Encoding code = Encoding.GetEncoding("gb2312");

            for (int i = 0; i < dt1.Rows.Count; i++)

            {

                string strModel = dt.Rows[0]["model_content"].ToString();

                StreamWriter sw = null;

                strModel = strModel.Replace("%title%", dt1.Rows[i][title].ToString());

                strModel = strModel.Replace("%content%", dt1.Rows[i][content].ToString());

                sw = new StreamWriter(HttpContext.Current.Server.MapPath("../" + dt1.Rows[i][strpath].ToString()), false, code);

                sw.Write(strModel);

                sw.Flush();

                sw.Close();

            }

        }

    }

 

}

 

 

 

转载于:https://www.cnblogs.com/wanheng/p/3297985.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值