ASP.NET怎样生成静态化页面 编辑器编辑后的就是带html标签的,将这个和newid等信息存入数据库,在前台用Querystring传入newsid参数.获取文章. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 生成静态页面你可以在编辑完成提交的时候同时进行.你可以做一个展示新闻的模板.html,然后replace里面的title.content,保存为一个静态的页面.html.同时将路径插入数据库 可以参考using system.net;using system.io; First:在服务器上指定aspx网页,生成html静态页public partial class Default2 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { StreamWriter sw = new StreamWriter(Server.MapPath("静态页1.htm"), false, System.Text.Encoding.GetEncoding("gb2312")); Server.Execute("Default3.aspx", sw); sw.Close(); } }}Second:在服务器上执行aspx网页时在page_render事件里将本页面生成html静态页public partial class Default3 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } protected override void Render(HtmlTextWriter writer) { StringWriter html = new StringWriter(); System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(html); base.Render(tw); System.IO.StreamWriter sw; sw = new System.IO.StreamWriter(Server.MapPath("静态页2.htm"), false, System.Text.Encoding.Default); sw.Write(html.ToString()); sw.Close(); tw.Close(); Response.Write(html.ToString()); }}Third:从指定连接获取源代码生成html静态页public partial class Default4 : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string pageurl = "http://www.baidu.com"; WebRequest request = WebRequest.Create(pageurl); WebResponse response = request.GetResponse(); Stream resstream = response.GetResponseStream(); StreamReader sr = new StreamReader(resstream, System.Text.Encoding.Default); string contenthtml = sr.ReadToEnd(); resstream.Close(); sr.Close(); //写入文件 System.IO.StreamWriter sw; sw = new System.IO.StreamWriter(Server.MapPath("静态页生成方法3.htm"), false, System.Text.Encoding.Default); sw.Write(contenthtml); sw.Close(); } }} 模板替换的方法http://www.chenjiliang.com/Article/View.aspx?ArticleID=3657&TypeID=5 模板页:NewsTemp.htm<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head> <title>NewsTemp</title></head><body style="text-align: center"> <table style="width: 659px; height: 283px"> <tr> <td colspan="4"> $hader$</td> </tr> <tr> <td colspan="1" style="width: 158px"> $left$</td> <td colspan="3"> $content$</td> </tr> <tr> <td colspan="4"> $feet$</td> </tr> </table></body></html>生成方法:#region 生成 /// <summary> /// 生成 /// </summary> /// <param name="content">内容</param> /// <param name="hader">页头</param> /// <param name="left">左侧导航</param> /// <param name="feet">页脚</param> /// <param name="path">存放路径</param> /// <returns></returns> public static bool CreateIndeax() { // 读取模板文件 string temp = HttpContext.Current.Server.MapPath("NewsTemp.htm"); StreamReader sr = null; StreamWriter sw = null; string str = ""; try { sr = new StreamReader(temp, Encoding.GetEncoding("UTF-8"));//编码根据实际设置 str = sr.ReadToEnd(); } catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); HttpContext.Current.Response.End(); sr.Close(); sw.Close(); } str = str.Replace("$hader$", hader);//替换相关标签 str = str.Replace("$left$", left); str = str.Replace("$feet$", feet); str = str.Replace("$content$", content); try { FileOperate.WriteFile(str, path);//FileOperate.文件操作,在这就不附了, return true; } catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); return false; } } #endregion 上面的没选效果,重发一次哈.模板页:NewsTemp.htm<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head> <title>NewsTemp</title></head><body style="text-align: center"> <table style="width: 659px; height: 283px"> <tr> <td colspan="4"> $hader$</td> </tr> <tr> <td colspan="1" style="width: 158px"> $left$</td> <td colspan="3"> $content$</td> </tr> <tr> <td colspan="4"> $feet$</td> </tr> </table></body></html>生成方法:#region 生成 /// <summary> /// 生成 /// </summary> /// <param name="content">内容</param> /// <param name="hader">页头</param> /// <param name="left">左侧导航</param> /// <param name="feet">页脚</param> /// <param name="path">存放路径</param> /// <returns></returns> public static bool CreateIndeax() { // 读取模板文件 string temp = HttpContext.Current.Server.MapPath("NewsTemp.htm"); StreamReader sr = null; StreamWriter sw = null; string str = ""; try { sr = new StreamReader(temp, Encoding.GetEncoding("UTF-8"));//编码根据实际设置 str = sr.ReadToEnd(); } catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); HttpContext.Current.Response.End(); sr.Close(); sw.Close(); } str = str.Replace("$hader$", hader);//替换相关标签 str = str.Replace("$left$", left); str = str.Replace("$feet$", feet); str = str.Replace("$content$", content); try { FileOperate.WriteFile(str, path);//FileOperate.文件操作,在这就不附了, return true; } catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); return false; } } #endregion 相关信息存在数据库,如果有修改之类的操作,从数据库读出替换新闻模板的$XXX$标签即可数据库新闻表Id Title Content HtmPath(存放htm路径,用于新闻列表显示.后期维护和修改) #region 生成 /// <summary> /// 生成 /// </summary> /// <param name="content">内容</param> /// <param name="hader">页头</param> /// <param name="left">左侧导航</param> /// <param name="feet">页脚</param> /// <param name="path">存放路径</param> /// <returns></returns> public static bool CreateIndeax(string content,string hader,string left,string feet,string path) {哈哈,之前丢参数了 http://topic.csdn.net/u/20080624/10/26a45062-572c-47e9-bb38-d9aa05b6c2ed.html 我前段时间也稍微研究过ASP.NET动态生成静态页面的方法,大致上来说有两种,一种是模板替换法,这个比较简单实用,一种是利用ASP.NET直接在服务器端运行目标文件,得到完整的HTML代码,将HTML代码保存在一个静态文件中,并向数据库中插入相关记录。这是目前我个人理解的,不知道有没有什么错误,或者可能有更好的方式,只是我不知道,呵呵。 [ align=center] asp.net 求两点间的坐标距离 跪求 ASP页面与ASP.NET页面之间的传值,不能在URL里面显示出来。 asp.net 下载后缀为.exe文件出错 在多个表中怎么查询同一关键字 [Petshop问题]这个在DAL层最底部分有一句(PetShop.IDAL.IProduct)Assembly.Load(path).CreateInstance(className);这一句表示什么意思 有关框架的页面 asp 和数据库的连接 将数据通过反射写入实体类的问题 解决问题,即刻揭帖!!! 如何让用户点中某个按钮后自动执行一段客户端程序? ajax中的responseXML 很难,很变态--关于用c#操作word里的shapes
可以参考
using system.net;
using system.io; First:在服务器上指定aspx网页,生成html静态页public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
StreamWriter sw = new StreamWriter(Server.MapPath("静态页1.htm"), false, System.Text.Encoding.GetEncoding("gb2312"));
Server.Execute("Default3.aspx", sw);
sw.Close();
}
}
}Second:在服务器上执行aspx网页时在page_render事件里将本页面生成html静态页public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void Render(HtmlTextWriter writer)
{
StringWriter html = new StringWriter();
System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(html);
base.Render(tw);
System.IO.StreamWriter sw;
sw = new System.IO.StreamWriter(Server.MapPath("静态页2.htm"), false, System.Text.Encoding.Default);
sw.Write(html.ToString());
sw.Close();
tw.Close();
Response.Write(html.ToString());
}
}Third:从指定连接获取源代码生成html静态页public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string pageurl = "http://www.baidu.com";
WebRequest request = WebRequest.Create(pageurl);
WebResponse response = request.GetResponse();
Stream resstream = response.GetResponseStream();
StreamReader sr = new StreamReader(resstream, System.Text.Encoding.Default);
string contenthtml = sr.ReadToEnd();
resstream.Close();
sr.Close(); //写入文件
System.IO.StreamWriter sw;
sw = new System.IO.StreamWriter(Server.MapPath("静态页生成方法3.htm"), false, System.Text.Encoding.Default);
sw.Write(contenthtml);
sw.Close();
}
}
}
http://www.chenjiliang.com/Article/View.aspx?ArticleID=3657&TypeID=5
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>NewsTemp</title>
</head>
<body style="text-align: center">
<table style="width: 659px; height: 283px">
<tr>
<td colspan="4">
$hader$</td>
</tr>
<tr>
<td colspan="1" style="width: 158px">
$left$</td>
<td colspan="3">
$content$</td>
</tr>
<tr>
<td colspan="4">
$feet$</td>
</tr>
</table></body>
</html>
生成方法:#region 生成
/// <summary>
/// 生成
/// </summary>
/// <param name="content">内容</param>
/// <param name="hader">页头</param>
/// <param name="left">左侧导航</param>
/// <param name="feet">页脚</param>
/// <param name="path">存放路径</param>
/// <returns></returns>
public static bool CreateIndeax()
{
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("NewsTemp.htm");
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(temp, Encoding.GetEncoding("UTF-8"));//编码根据实际设置
str = sr.ReadToEnd();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
sr.Close();
sw.Close();
}
str = str.Replace("$hader$", hader);//替换相关标签
str = str.Replace("$left$", left);
str = str.Replace("$feet$", feet);
str = str.Replace("$content$", content); try
{
FileOperate.WriteFile(str, path);//FileOperate.文件操作,在这就不附了,
return true;
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
return false;
} }
#endregion
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>NewsTemp</title>
</head>
<body style="text-align: center">
<table style="width: 659px; height: 283px">
<tr>
<td colspan="4">
$hader$</td>
</tr>
<tr>
<td colspan="1" style="width: 158px">
$left$</td>
<td colspan="3">
$content$</td>
</tr>
<tr>
<td colspan="4">
$feet$</td>
</tr>
</table></body>
</html>
生成方法:#region 生成
/// <summary>
/// 生成
/// </summary>
/// <param name="content">内容</param>
/// <param name="hader">页头</param>
/// <param name="left">左侧导航</param>
/// <param name="feet">页脚</param>
/// <param name="path">存放路径</param>
/// <returns></returns>
public static bool CreateIndeax()
{
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("NewsTemp.htm");
StreamReader sr = null;
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(temp, Encoding.GetEncoding("UTF-8"));//编码根据实际设置
str = sr.ReadToEnd();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
sr.Close();
sw.Close();
}
str = str.Replace("$hader$", hader);//替换相关标签
str = str.Replace("$left$", left);
str = str.Replace("$feet$", feet);
str = str.Replace("$content$", content); try
{
FileOperate.WriteFile(str, path);//FileOperate.文件操作,在这就不附了,
return true;
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
return false;
} }
#endregion
/// <summary>
/// 生成
/// </summary>
/// <param name="content">内容</param>
/// <param name="hader">页头</param>
/// <param name="left">左侧导航</param>
/// <param name="feet">页脚</param>
/// <param name="path">存放路径</param>
/// <returns></returns>
public static bool CreateIndeax(string content,string hader,string left,string feet,string path)
{
哈哈,之前丢参数了
这是目前我个人理解的,不知道有没有什么错误,或者可能有更好的方式,只是我不知道,呵呵。