当前位置:编程学习 > C#/ASP.NET >>

asp.net生成静态页面

我急需asp.net生成静态页面的例子代码,有谁能给我做个这样的例子啊。不要编译过的 不要用什么工具编译好的  要手写的加上注释本人学的不是很好  还请高手给个例子  邮箱:zhengyuxiang_1987@yahoo.com.cn  能留下QQ指导我那就实在太感谢啦 --------------------编程问答-------------------- Asp.NET生成静态页面并分页
http://topic.csdn.net/u/20080624/10/26a45062-572c-47e9-bb38-d9aa05b6c2ed.html?66281

1、静态模板页面 template.html,主要是定义了一些特殊字符,用来被替换。 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<title>Title </title> 
</head> 
<body> 
<div style="width: 417px; height: 54px" align="center"> 
<br /> 
Title </div> 
<div style="width: 417px; height: 8px"> 
浏览 <font color="red"> <script src="http://localhost/.Net/NewsFiles/ClickCount.aspx?NewsId=NewsId"> </script> </font>次 Time </div> 
<div style="width: 417px; height: 100px"> 
Content </div> 
<div style="width: 416px; height: 9px"> 
Pager </div> 
<div style="width: 416px; height: 8px"> 
<form id="form1" action="../AddComment.aspx" style="margin:0px"> 
<input id="Text1" type="text" /> <Img id="Image1" src="http://www.dwww.cn/UserInfo/CheckCode.aspx"/> <br /> 
<textarea id="CommentContent" cols="20" rows="2"> </textarea> 
<br /> 
<input id="NewsId" type="hidden" value="NewsId"/> 
<input id="Button1" type="submit" value="button" /> 
<a href="http://www.dwww.cn/News/Display.aspx?NewsId=NewsId">查看更多评论 </a> </form> 
</div> 
</body> 
</html> 


2、前态页面 NewsAdd.aspx,就是一个表单,用来填写新闻的标题和内容。 

<%@ Page Language="C#" AutoEventWireup="false" validateRequest="false" CodeFile="NewsAdd.aspx.cs" Inherits="NewsAdd.Admin_AdminPanel_NewsAdd" %> 
<%@ Register TagPrefix="FCKeditorV2" Namespace="FredCK.FCKeditorV2" Assembly="FredCK.FCKeditorV2" %> 
<!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 runat="server"> 
<title>添加新闻 </title> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
<asp:Label ID="Label2" runat="server" Text="标题"> </asp:Label> 
<asp:TextBox ID="Title" runat="server" Width="325px"> </asp:TextBox> <br /> 
<asp:Label ID="Label1" runat="server" Text="内容"> </asp:Label> 
<FCKeditorV2:FCKeditor id="Content" basePath="~/FCKeditor/" runat="server" Height="400px" Width="70%"> </FCKeditorV2:FCKeditor> 
<asp:Button ID="Button1" runat="server" onClick="Button1_Click" Text="Button" /> 
<asp:Label ID="Message" runat="server" > </asp:Label> </div> 
</form> 
</body> 
</html> 


3、后台页面 NewsAdd.aspx.cs 

using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
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 Mysqlserver; 
using System.IO; 
using System.Text; 
namespace NewsAdd 

public partial class Admin_AdminPanel_NewsAdd : System.Web.UI.Page 

protected void Page_Load(object sender, EventArgs e) 



protected void Button1_Click(object sender, EventArgs e) 

string strDate = DateTime.Now.ToString("yyMMdd") + "\" + DateTime.Now.ToString("yyyymmddhhmmss"); 
string strFileName = strDate + ".shtml";//存储到数据库中 
string strTitle=Request.Form["Title"].ToString().Trim();//接收传过来的标题 
string strContent=Request.Form["Content"].ToString().Trim();//接收传过来的内容 
string[] content = strContent.Split(new Char[] {'|'});//对内容进行拆分,并保存到数组 
int upbound = content.Length;//数组的上限 
SqlServerDataBase db = new SqlServerDataBase(); 
bool success = db.Insert("insert into inNews(Title,Content,FilePath)values('" + strTitle + "','" + strContent + "','" + strFileName + "')", null); 
//if (success) 
// Message.Text = "添加成功!"; 
/**////////////////////////////创建当前日期的文件夹开始 
string dir = Server.MapPath("../../"+"NewsFiles/"+DateTime.Now.ToString("yyMMdd"));//用来生成文件夹 
if (!Directory.Exists(dir)) 

Directory.CreateDirectory(dir); 

/**////////////////////////////创建当前日期的文件夹结束 
try 

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

//string[] newContent = new string[4];//定义和html标记数目一致的数组 
StringBuilder strhtml = new StringBuilder(); 

//创建StreamReader对象 
using (StreamReader sr = new StreamReader(Server.MapPath("../../" + "NewsFiles/") + "\template.html",Encoding.GetEncoding("gb2312"))) 

String oneline; 
//读取指定的HTML文件模板 
while ((oneline = sr.ReadLine()) != null) 

strhtml.Append(oneline); 

sr.Close(); 


//为标记数组赋值 
//SqlServerDataBase db = new SqlServerDataBase(); 
DataSet ds = db.Select("select top 1 NewsId from inNews order by NewsId desc", null);//获取id 
string strTable = " <table> <tr> <td>upUrl </td> <td>Number </td> <td>downUrl </td> </tr> </table>";//上下页表格,注意此处的upUrl(上一页),Number(页码分页),downUrl(下一页) 
//这三个是用来替换的。 

string FilePath=""; 
strhtml = strhtml.Replace("Title", strTitle); 
strhtml = strhtml.Replace("NewsId", ds.Tables[0].Rows[0]["NewsId"].ToString()); 
strhtml = strhtml.Replace("Time", DateTime.Now.ToString("yyyy/MM/dd")); 
strhtml = strhtml.Replace("Content", content[i]); 
string strNumber = "";//数字分页1,2,3…… 
for (int m = 1; m <=upbound; m++) 

if (m == 1)//如果是第一页就显示成这个样子:20070524.shtml而不是20070524_1.shtml 
strNumber = strNumber + " ["+" <a href=" + "../" + strDate + ".shtml" + ">" + m + " </a>"+"] "; 
else 

int n = m - 1;//第三页的连接应该是20070524_2.shtml,以此类推 
strNumber = strNumber + " [" +" <a href=" + "../" + strDate + "_" + n + ".shtml" + ">" + m + " </a>"+"] "; 


if (upbound == 0)//如果没有分页,就直接按日期时间保存 

FilePath = Server.MapPath("../../") + "NewsFiles" + "//" + strDate + ".shtml"; 
strhtml = strhtml.Replace("Pager", ""); 

else//否则按20070524.shtml、20070524_1.shtml 这种效果保存 

if (i == 0) 
FilePath = Server.MapPath("../../") + "NewsFiles" + "//" + strDate + ".shtml"; 
else 
FilePath = Server.MapPath("../../") + "NewsFiles" + "//" + strDate + "_" + i + ".shtml"; 

if (i == 0)//第一页不显示上一页 
strTable = strTable.Replace("upUrl", ""); 

if (i <= 1)//上一页分页 
strTable = strTable.Replace("upUrl", " <a href=" + "../" + strDate + ".shtml" + ">上一页 </a>"); 
else 

int p = i - 1; 
strTable = strTable.Replace("upUrl", " <a href=" + "../" + strDate + "_" + p + ".shtml" + ">上一页 </a>"); 


if(upbound==1)//如果只有一页,则不显示页码 
//strNumber=""; 
strTable = strTable.Replace("Number", ""); 
else 
strTable = strTable.Replace("Number", strNumber);//页码替换 
/**///////////////////////// 
if(i==upbound-1)//最后一页不显示下一页 
strTable = strTable.Replace("downUrl", ""); 

if (i != upbound - 1)//下一页分页 

int q = i + 1; 
strTable = strTable.Replace("downUrl", " <a href=" + "../" + strDate + "_" + q + ".shtml" + ">下一页 </a>"); 

else 

int j = upbound - 1; 
strTable = strTable.Replace("downUrl", " <a href=" + "../" + strDate + "_" + j + ".shtml" + ">下一页 </a>"); 


strhtml = strhtml.Replace("Pager", strTable); 

//创建文件信息对象-------------------------------------------- 
FileInfo finfo = new FileInfo(FilePath); 
//以打开或者写入的形式创建文件流 
using (FileStream fs = finfo.OpenWrite()) 

//根据上面创建的文件流创建写数据流 
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); 
//把新的内容写到创建的HTML页面中 
sw.WriteLine(strhtml); 
sw.Flush(); 
sw.Close(); 



catch (Exception err) 

//输出异常信息 
Response.Write(err.ToString()); 






请不要直接拷贝使用,里面的路径需要更改,但程序绝对没问题,在我本地已经测试通过。 

另外在使用时,比如我要把新闻的内容分成4页,就应该这样写:111|222|333|444。 
--------------------编程问答-------------------- 按照模板生成或用伪静态。
参考
参考
参考 --------------------编程问答-------------------- 一楼的分数很想给你啊  可是我现在只有17分啦  有分了在给你  谢谢 --------------------编程问答-------------------- 一楼的  仔细看才知道是网上复制的啊   郁闷 --------------------编程问答-------------------- 把“获得html”跟过时的所谓字符串替换程序联系起来,未免是把人怪带到阴沟里去了。输出html在asp.net中是直截了当的,你的网站可以模拟用户第一次访问一个页面而输出为html,例如一下这个aspx:
<%@ Page Language="C#" %>

<%@ Import Namespace="System.IO" %>

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        StringWriter wr = new StringWriter();
        Server.Execute(this.TextBox1.Text, wr);     //你可以使用第三个参数传递页面的更多初始数据
        this.Label1.Text = Server.HtmlEncode(wr.ToString());
        File.WriteAllText(Server.MapPath(this.TextBox2.Text), wr.ToString());
    }
</script>

<!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 runat="server">
    <title>演示取得当前网站中其它网页的源代码只需要两行代码就够了</title>
</head>
<body>
    <form id="form1" runat="server" defaultbutton="Button1">
    <div>
        请输入本网站的页面名称:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        请输入目标文件名称<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><hr />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>


你可以把这个aspx中的那几行代码写成一个方法,可以在你的网站中把本网站任何页面写出html到你指定的磁盘目录里。 --------------------编程问答-------------------- 这里只用3条语句就将网站任何aspx页面输出成为html文件,跟如何实现aspx内部根本没有任何关系。

asp.net系统只要写一行代码设置页面输出缓存就可以智能输出html,只不过html缓存在内存中,仅仅占很小的几乎可以忽略不计的内存空间。 --------------------编程问答-------------------- 按照静态界面的格式 在程序中写就好 --------------------编程问答-------------------- 有时候使用这三行代码中的前两行,然后把内容直接灌输到电子邮件内容中。而asp.net网站本身则是以输出缓存为手段。

这个只有两行地代码,是一个很简单的asp.net编程知识,只是用得少罢了。 --------------------编程问答-------------------- 学习! --------------------编程问答-------------------- 举手都来不及。。 --------------------编程问答--------------------
引用 4 楼 dsafdsaf 的回复:
一楼的  仔细看才知道是网上复制的啊   郁闷


有什么好郁闷的?那个是这个ASP.NET版块的推荐文章,我也用过了。 --------------------编程问答-------------------- 哎,看不明白 --------------------编程问答-------------------- 下面不是有一张帖子吗? --------------------编程问答-------------------- up --------------------编程问答--------------------
引用 5 楼 sp1234 的回复:
把“获得html”跟过时的所谓字符串替换程序联系起来,未免是把人怪带到阴沟里去了。输出html在asp.net中是直截了当的,你的网站可以模拟用户第一次访问一个页面而输出为html,例如一下这个aspx:
HTML code<%@ Page Language="C#" %>

<%@ Import Namespace="System.IO" %>

<script runat="server">
    protected void Button1_Click(object sender, EventArgs e)
    {
        StringWriter wr = new StringWriter()…


很好很简的例子 --------------------编程问答-------------------- 首先我想说 这个是我个人不成熟看法 :静态化看楼主网站规模,如果是中小型网站 用缓存可能就能满足需求,而且中小型网站不管哪种静态化方法都是能满足需求的 
关于静态化分页: 
一般都是直接截取Stream流写入HTML文件的方法,缺点是:1,维护成本太高 如果有修改,那么以前的静态页面全部得重新生成,  2.发表一个新项时(比如新闻),那么就得对应生成1个或者几个相应页面,那么CPU的占用很高 服务器压力比较大 

试想:很多大网站初期的内容很多都是直接采集过来 这种方法我觉得的不可取  

我是使用这个方法博客园  就和sp1234的思路是一样的 但是具体化了
http://www.cnblogs.com/hsqzzzl/archive/2005/10/23/260122.html  代码很完整 
.使用实现IHttpHnder接口,实现ProcessRequest方法,对每次请求的URL进行判断,需重写的页面路径放入XML文件中,每次判断,有则指向, 
无则执行页面(这里就是普通处理页面),生成静态页面,访问生成的静态页面, 
通过文件的最后修改时间每次判断是否要重新生产 
这样解决的话  分页静态化也就解决了  优点是:好处理 维护相对上面方法比较好 服务器压力减少 缺点是:第一次访问速度慢 
希望能帮到你~  --------------------编程问答-------------------- <tr bgcolor="#0fffff"><td>无聊啊</td></tr> --------------------编程问答-------------------- <script>
alert('shuru');
</script> --------------------编程问答--------------------
<tr bgcolor="#0fffff"> <td>无聊啊 </td> </tr>
--------------------编程问答-------------------- 哎   我真的佩服了  能弄个全一点的中不?不是缺胳膊就是少腿的 --------------------编程问答-------------------- mark --------------------编程问答-------------------- 怎么了就没有一份完整的源码呢 --------------------编程问答-------------------- 在asp.net基类里写的方法:

public void CreateHTML(System.Web.UI.Page p,string sourceurl, string url)
    {
        //System.Net.WebRequest myRequest = System.Net.WebRequest.Create("http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port + sourceurl);
        System.Net.WebRequest myRequest = System.Net.WebRequest.Create("http://" + HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port +p.ResolveUrl("~") + sourceurl);
        System.Net.WebResponse myResponse = myRequest.GetResponse();
        Stream stream = myResponse.GetResponseStream();
        StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
        StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath(url), false, System.Text.Encoding.Default);
        sw.WriteLine(sr.ReadToEnd());
        sw.Close();

    } 

调用时:CreateHTML(Page,字符1,字符2);
--------------------编程问答-------------------- --------------------编程问答-------------------- --------------------编程问答-------------------- 哎  郁闷呢 --------------------编程问答-------------------- 看着很麻烦的样子 --------------------编程问答-------------------- 就是把生成出来的东西放到模板的标签中 --------------------编程问答--------------------
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,