所用到的类:TOHTML.cs
————————————————————
using System;
using System.Collections.Generic;
using System.Web;
using System.IO;
using System.Data;
using System.Text;
/// <summary>
///TOHTML 的摘要说明
/// </summary>
public class TOHTML
{
/// <summary>
/// 读取htm文件内容
/// </summary>
/// <param name="strfile">文件路径</param>
/// <returns></returns>
public string GetHtml(string strfile)
{
string strout = "false";
try
{
string strfilepath = System.Web.HttpContext.Current.Server.MapPath(strfile);
//string strfilepath = strfile;
strout = "";
if (!File.Exists(strfilepath))//System.Web.HttpContext.Current.Server.MapPath(strfile)
{
strout = "false";
}
else
{
StreamReader sr = new StreamReader(strfilepath, System.Text.Encoding.UTF8);
string input = sr.ReadToEnd();
sr.Close();
strout = input;
}
}
catch
{
//log.Debug("DatAccess_GetInterIDList_1", ex);
}
return strout;
}
/// <summary>
/// 创建html文件
/// </summary>
/// <param name="strFileName">文件路径文件夹名称</param>
/// <param name="strText">文件内容</param>
/// <returns></returns>
public string CreateHtml(string filePath, string fileName, string strText)
{
string pathWith = "";
string withFilePath = "";
withFilePath = filePath;
try
{
//创建文件夹
if (!Directory.Exists(withFilePath))
{
Directory.CreateDirectory(withFilePath);
}
}
catch (Exception ex)
{
}
withFilePath += fileName;
string strbol = "false";
try
{
if (!File.Exists(withFilePath))
{
StreamWriter SW;
SW = File.CreateText(withFilePath);
SW.Close();
//strText = "aaa";
File.AppendAllText(withFilePath, strText, Encoding.UTF8);
}
else
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(withFilePath, false, Encoding.UTF8);
sw.Write(strText);
sw.Close();
}
strbol = "true";
}
catch
{
//log.Debug("DatAccess_HtmlWenJian_2", ex);
}
return strbol;
}
}
——————————————————————————
执行/处理页面静态化
//生成单击事件
protected void btnToHtml(object sender, EventArgs e)
{
//此注释是绝对路径大家可以先试下是否能用能用在改用下面的相对路径
//TOHTML tohtml = new TOHTML();
//string sss = tohtml.GetHtml("E:\\web\\index.aspx");
//tohtml.GetHtml("E:\\web\\News.htm");//模板页
//tohtml.CreateHtml("E:\\", "aaa.html", sss);
//此部分是相对路径处理
string id;
if (string.IsNullOrEmpty(txtID.Text))
{
id = "1";//aspx?id=11 为例子 此id即11。如果为空默认执行1.大家可以用传参数来获取
}
else
{
id = txtID.Text;
}
this.Binds(id);//查询所需要替换的文本内容
TOHTML tohtml = new TOHTML();
string mubanHtml = txtmubanHtml.Text;//模板页比如index.htm,new_show.htm
string NewAspx = txtNewaspx.Text;//需生成页面如index.aspx,new_show.aspx
string NewHTmlName = txtNewaspx.Text.Substring(0, txtNewaspx.Text.LastIndexOf(".aspx")) + id + ".htm";//生成新静态的名字
//NewHTmlName = NewHTmlName + "?id=" + id;//一个参数的
//NewHTmlName = NewHTmlName + "?id=" + id + "&ty=abc";//多个参数的
string strcon = tohtml.GetHtml("../" + mubanHtml);//获取模板页内容保存string类型
//替换内容 模板页怎么弄具体百度去
strcon = strcon.Replace("###Title###", TextBox1.Text);
strcon = strcon.Replace("###Connent###", TextBox2.Text);
tohtml.CreateHtml(Server.MapPath("~/"), NewHTmlName, strcon);//创建该文件
Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('生成成功')", true);
}
//替换内容
protected void Binds(string id)
{
string sql="select * from [News] where id="+Convert.ToInt32(id)+"";
DataTable dt = DBHelper.GetDataTable(sql);
TextBox1.Text = dt.Rows[0]["Title"].ToString();
TextBox2.Text = dt.Rows[0]["Connent"].ToString();
}
————————————————————————
URL:访问index.aspx的时候 我们先让他判断是否存在indexhtml 存在跳转到index.html 不存在不执行操作
即还是访问index.aspx URL重写和伪静态暂未研究。搜索目录是否存在耗性能 多还是每次打开数据库耗性能
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.Data.OleDb;
using System.IO;
public class KeyTitle : System.Web.UI.Page
{
public KeyTitle()
{
this.Load += new EventHandler(KBinds);
}
protected void KBinds(object sender, EventArgs e)
{
this.toHtml();
string sql = "select * from [keyTitle]";
OleDbDataReader dr = DBHelper.GetDataReader(sql);
if (dr.Read())
{
//HtmlHead head = this.Header;//获得Head标记
HtmlHead head = Page.Header;
head.Title = dr["Title"].ToString();
HtmlMeta WebKeyWords = new HtmlMeta();//创建Meta标记
WebKeyWords.Name = "KeyWords";
WebKeyWords.Content = dr["keywords"].ToString();
head.Controls.Add(WebKeyWords); //向head里添加Meta
HtmlMeta WebDescription = new HtmlMeta();
WebDescription.Name = "Description";
WebDescription.Content = dr["Des"].ToString();
head.Controls.Add(WebDescription);
dr.Close();
}
}
protected void toHtml()
{
string id = Request.QueryString["id"];
if (string.IsNullOrEmpty(id))
{
id = "11";
}
string URL = Request.Url.ToString();//网址
string htmlurl = URL.Substring(0, URL.LastIndexOf(".aspx")) + id + ".htm";//aspx后缀名改成html
string path = Server.MapPath("~/") + htmlurl.Substring(htmlurl.LastIndexOf("/") + 1);
if (File.Exists(path))//判断是否存在该文件
{
Response.Redirect(htmlurl);//存在跳转
}
}
}
未归类