关于怎样生成静态页 关于怎样生成静态页 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1.用模板,设计好模板,设置好各种标记,然后用内容替换标记2.用HttpWebRequest请求动态页面,得到html字符串然后写入文件 下面的示例可能对楼主有帮助;这是CreateHtml.aspx文件代码:<%@ Import Namespace="System.IO" %><%@ Import Namespace="System.Text" %><script runat="server" language="vb">'该过程用来生成HTML网页Sub Enter_Click(Sender As System.Object, E As System.EventArgs) '下面首先读取模板文件,将其中的全部代码保存到一个字符串中 Dim strTemplate As String Dim sr As New StreamReader(Server.MapPath("template.htm"), Encoding.GetEncoding("gb2312")) strTemplate=sr.ReadToEnd() sr.Close() '下面利用客户输入的内容替换模板中的相应字符串 strTemplate=strTemplate.Replace("$title$",txtTitle.Text) strTemplate=strTemplate.Replace("$body$",txtBody.Text) strTemplate=strTemplate.Replace("$SubmitDate$",Now()) '下面建立一个StreamWriter对象,生成HTML文件 Dim sw As New StreamWriter(Server.MapPath(GetFileName()), False, Encoding.GetEncoding("gb2312")) sw.Write(strTemplate) sw.Close() '下面给出一个浏览超链接 message.Text = "<a href='" & GetFileName() & "' target='_blank'>请查看生成的文件</a>" End Sub'该函数用来返回由当前时间组成的文件名称Function GetFileName() GetFileName = Now().ToString.Replace("-","").Replace(":","").Replace(" ","") GetFileName = GetFileName & ".htm"End Function</script><HTML><body> <h3 align="center">利用模板生成HTML文件</h3> <form runat="server"> <table width="80%" align="center"> <tr> <td width="30%" align="right">标题</td> <td><asp:Textbox id="txtTitle" columns="50" runat="server"/></td> </tr> <tr> <td width="30%" align="right">内容</td> <td><asp:Textbox id="txtBody" TextMode="MultiLine" rows="8" columns="40" runat="server"/></td> </tr> <tr> <td colspan="2" align="center"><asp:Button id="Enter" Text="确定" Runat="server" OnClick="Enter_Click"/></td> </tr> </table> <asp:Label id="message" Runat="server" /> </form></BODY></HTML>这是模版文件代码template.htm:<html><head> <title>$title$</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body> <h3 align="center">$title$</h3> <table height="80%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px solid #000000"> <tr valign="top"> <td>$body$</td> </tr> </table> <p align="center">信息中心 $SubmitDate$</p></body></html> strTemplate=strTemplate.Replace("$body$",txtBody.Text)——————————————————————————————————————————这个让用户在网页上用TextBox输入,那么与任何门户网站相比,有什么实用价值吗? 写的这么详细应该可以解决你的问题了吧,别忘了揭贴时给分哦,^_^private void btnCreate_Click(object sender, System.EventArgs e){string[] newContent = new string[5];//定义和html标记数目一致的数组StringBuilder strhtml = new StringBuilder();try { //创建StreamReader对象 using (StreamReader sr = new StreamReader(Server.MapPath("createHTML")+ "\\template.html")) { String oneline; //读取指定的HTML文件模板 while ((oneline = sr.ReadLine()) != null) { strhtml.Append(oneline); } sr.Close(); }}catch(Exception err){//输出异常信息Response.Write(err.ToString());}//为标记数组赋值newContent[0] = txtTitle.Text;//标题newContent[1] = "BackColor='#cccfff'";//背景色newContent[2] = "#ff0000";//字体颜色newContent[3] = "100px";//字体大小newContent[4] = txtContent.Text;//主要内容//根据上面新的内容生成html文件try{//指定要生成的HTML文件string fname = Server.MapPath("createHTML") +"\\" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";//替换html模版文件里的标记为新的内容for(int i=0;i < 5;i++){ strhtml.Replace("$htmlkey["+i+"]",newContent[i]);}//创建文件信息对象 FileInfo finfo = new FileInfo(fname);//以打开或者写入的形式创建文件流 using(FileStream fs = finfo.OpenWrite()) {//根据上面创建的文件流创建写数据流StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));//把新的内容写到创建的HTML页面中sw.WriteLine(strhtml);sw.Flush();sw.Close();}//设置超级链接的属性hyCreateFile.Text = DateTime.Now.ToString("yyyymmddhhmmss")+".html";hyCreateFile.NavigateUrl = "createHTML/"+DateTime.Now.ToString("yyyymmddhhmmss")+".html";}catch(Exception err){ Response.Write (err.ToString());}} //生成HTML页 public static bool WriteFile(string strText,string strContent,string strAuthor) { string path = HttpContext.Current.Server.MapPath("/news/"); Encoding code = Encoding.GetEncoding("gb2312"); // 读取模板文件 string temp = HttpContext.Current.Server.MapPath("/news/text.html"); StreamReader sr=null; StreamWriter sw=null; string str=""; try { sr = new StreamReader(temp, code); str = sr.ReadToEnd(); // 读取文件 } catch(Exception exp) { HttpContext.Current.Response.Write(exp.Message); HttpContext.Current.Response.End(); sr.Close(); } string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html"; // 替换内容 // 这时,模板文件已经读入到名称为str的变量中了 str =str.Replace("ShowArticle",strText); //模板页中的ShowArticle str = str.Replace("biaoti",strText); str = str.Replace("content",strContent); str = str.Replace("author",strAuthor); // 写文件 try { sw = new StreamWriter(path + htmlfilename , false, code); sw.Write(str); sw.Flush(); } catch(Exception ex) { HttpContext.Current.Response.Write(ex.Message); HttpContext.Current.Response.End(); } finally { sw.Close(); } return true; 此函数放在Conn.CS基类中了 在添加新闻的代码中引用 注:工程名为Hover if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString))) { Response.Write("添加成功"); } else { Response.Write("生成HTML出错!"); } 模板页Text.html代码 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>ShowArticle</title> <body> biaoti <br> content<br> author </body> </HTML> biaoti <br> content<br> author </body> </HTML> 提示添加成功后会出以当前时间为文件名的html文件!上面只是把传递过来的几个参数直接写入了HTML文件中,在实际应用中需要先添加数据库,然后再写入HTML文件 通过在basePage重载OnInit方法(判断aspx对应的静态页面是否存在,来决定是生成还是重定向)和Render方法(到这里了可以确定页面是不存在的,所以把要生成的内容流存到静态页面中去,而不是直接render到客户浏览器,最后重定向)。在内容没有更新时,页面请求都会被OnInit重定向到静态页面(也可以确定是最新内容)。当有内容更新的同时,删除内容相关的所有静态页面(可动态配置),达到更新静态页面的目的。另外注意一点的是,我的实际使用中,因为不同页面定位的路径不一样,所以我写了一个BasePage,还有多个moduleBasePage,最后页面是继承的moduleBasePage,我想你也会遇到一样的问题。 修改了 C:\WINDOWS\system32\drivers\etc\host IIS发布的问题(100分) 大写字母转换成小写 petshop为什么要分成4个数据库文件? 怎么在gridview里显示其绑定的表格的列名?即表格字段名? 服务器标记解析不正确的问题 打开WORD文档 关于制作留言版[我是美工T_T] ASP.net中DataGrid问题 关于数据库的问题 怎样分割一个文件保存为两个文件,又将这两个文件还原为一个文件? 一个新闻系统中,怎样把内容写入文件夹里,而不是写入数据库,这个文件夹以这个新闻的标题命名,读的时候还能从这里面读出内容
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<script runat="server" language="vb">
'该过程用来生成HTML网页
Sub Enter_Click(Sender As System.Object, E As System.EventArgs)
'下面首先读取模板文件,将其中的全部代码保存到一个字符串中
Dim strTemplate As String
Dim sr As New StreamReader(Server.MapPath("template.htm"), Encoding.GetEncoding("gb2312"))
strTemplate=sr.ReadToEnd()
sr.Close()
'下面利用客户输入的内容替换模板中的相应字符串
strTemplate=strTemplate.Replace("$title$",txtTitle.Text)
strTemplate=strTemplate.Replace("$body$",txtBody.Text)
strTemplate=strTemplate.Replace("$SubmitDate$",Now())
'下面建立一个StreamWriter对象,生成HTML文件
Dim sw As New StreamWriter(Server.MapPath(GetFileName()), False, Encoding.GetEncoding("gb2312"))
sw.Write(strTemplate)
sw.Close()
'下面给出一个浏览超链接
message.Text = "<a href='" & GetFileName() & "' target='_blank'>请查看生成的文件</a>"
End Sub
'该函数用来返回由当前时间组成的文件名称
Function GetFileName()
GetFileName = Now().ToString.Replace("-","").Replace(":","").Replace(" ","")
GetFileName = GetFileName & ".htm"
End Function
</script>
<HTML>
<body>
<h3 align="center">利用模板生成HTML文件</h3>
<form runat="server">
<table width="80%" align="center">
<tr>
<td width="30%" align="right">标题</td>
<td><asp:Textbox id="txtTitle" columns="50" runat="server"/></td>
</tr>
<tr>
<td width="30%" align="right">内容</td>
<td><asp:Textbox id="txtBody" TextMode="MultiLine" rows="8" columns="40" runat="server"/></td>
</tr>
<tr>
<td colspan="2" align="center"><asp:Button id="Enter" Text="确定" Runat="server" OnClick="Enter_Click"/></td>
</tr>
</table>
<asp:Label id="message" Runat="server" />
</form>
</BODY>
</HTML>
这是模版文件代码template.htm:<html>
<head>
<title>$title$</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<h3 align="center">$title$</h3>
<table height="80%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee"
style="border:1px solid #000000">
<tr valign="top">
<td>$body$</td>
</tr>
</table>
<p align="center">信息中心 $SubmitDate$</p>
</body>
</html>
——————————————————————————————————————————
这个让用户在网页上用TextBox输入,那么与任何门户网站相比,有什么实用价值吗?
private void btnCreate_Click(object sender, System.EventArgs e)
{
string[] newContent = new string[5];//定义和html标记数目一致的数组
StringBuilder strhtml = new StringBuilder();
try
{
//创建StreamReader对象
using (StreamReader sr = new StreamReader(Server.MapPath("createHTML")+ "\\template.html"))
{
String oneline;
//读取指定的HTML文件模板
while ((oneline = sr.ReadLine()) != null)
{
strhtml.Append(oneline);
}
sr.Close();
}
}
catch(Exception err)
{
//输出异常信息
Response.Write(err.ToString());
}
//为标记数组赋值
newContent[0] = txtTitle.Text;//标题
newContent[1] = "BackColor='#cccfff'";//背景色
newContent[2] = "#ff0000";//字体颜色
newContent[3] = "100px";//字体大小
newContent[4] = txtContent.Text;//主要内容//根据上面新的内容生成html文件
try
{
//指定要生成的HTML文件
string fname = Server.MapPath("createHTML") +"\\" + DateTime.Now.ToString("yyyymmddhhmmss") + ".html";
//替换html模版文件里的标记为新的内容
for(int i=0;i < 5;i++)
{
strhtml.Replace("$htmlkey["+i+"]",newContent[i]);
}
//创建文件信息对象
FileInfo finfo = new FileInfo(fname);
//以打开或者写入的形式创建文件流
using(FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter sw = new StreamWriter(fs,System.Text.Encoding.GetEncoding("GB2312"));
//把新的内容写到创建的HTML页面中
sw.WriteLine(strhtml);
sw.Flush();
sw.Close();
}
//设置超级链接的属性
hyCreateFile.Text = DateTime.Now.ToString("yyyymmddhhmmss")+".html";
hyCreateFile.NavigateUrl = "createHTML/"+DateTime.Now.ToString("yyyymmddhhmmss")+".html";
}
catch(Exception err)
{
Response.Write (err.ToString());
}
}
public static bool WriteFile(string strText,string strContent,string strAuthor)
{
string path = HttpContext.Current.Server.MapPath("/news/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("/news/text.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str =str.Replace("ShowArticle",strText); //模板页中的ShowArticle
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 写文件
try
{
sw = new StreamWriter(path + htmlfilename , false, code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
此函数放在Conn.CS基类中了
在添加新闻的代码中引用 注:工程名为Hover
if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出错!");
}
模板页Text.html代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ShowArticle</title>
<body>
biaoti
<br>
content<br>
author
</body>
</HTML>
biaoti
<br>
content<br>
author
</body>
</HTML>
提示添加成功后会出以当前时间为文件名的html文件!上面只是把传递过来的几个参数直接写入了HTML文件中,在实际应用中需要先添加数据库,然后再写入HTML文件