ASP.Net/asp 生成静态HTML页

80酷酷网    80kuku.com

  asp.net|静态

环境:Microsoft .NET Framework SDK v1.1

OS:Windows Server 2003 中文版

ASP.Net生成静态HTML页

在Asp中实现的生成静态页用到的FileSystemObject对象!

在.Net中涉及此类操作的是System.IO

以下是程序代码 注:此代码非原创!参考别人代码

//生成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); file://模板页中的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

 


 content


 author

 </body>

</HTML>


------------------------------------------------------------------------

提示添加成功后会出以当前时间为文件名的html文件!上面只是把传递过来的几个参数直接写入了HTML文件中,在实际应用中需要先添加数据库,然后再写入HTML文件

而且需要把生成的文件名等写入数库以便以后调用等,此实例只是实现了根据提交过来参数替换模板中的相应的字段! 需要完善的地方很多!哪位有高见,欢迎赐教!




ASP.Net生成静态HTML页! 2004-06-01 13:36 Kriss

我找到我想用的方法了:

using System.Web.Hosting;



WebHost host = (WebHost)ApplicationHost.CreateApplicationHost(

typeof(WebHost), name, path);



public void DoRequest(string page, string query, TextWriter writer) {

HttpRuntime.ProcessRequest(new SimpleWorkerRequest(page, query, writer));

}


这样我就可以直接把一个ASPX文件产生HTML文件了

FileStream fs = File.Open("Default.htm");

host.DoRequest("Default.aspx",string.Empty,fs); 

ASP生成静态网页的方法 


随着网站访问量的加大,每次从数据库读取都是以效率作为代价的,很多用ACCESS作数据库的更会深有体会,静态页加在搜索时,也会被优先考虑。互联网上流行的做法是将数据源代码写入数据库再从数据库读取生成静态面,这样无形间就加大了数据库。将现有的ASP页直接生成静态页,将会节省很多。


  下面的例子是将、index.asp?id=1/index.asp?id=2/index.asp?id=3/这三个动态页面,分别生成ndex1.htm,index2.htm,index3.htm存在根目录下面:


<%

dim strUrl,Item_Classid,id,FileName,FilePath,Do_Url,Html_Temp

Html_Temp="<UL>"

For i=1 To 3

Html_Temp = Html_Temp&"<LI>"

Item_Classid = i

FileName = "Index"&Item_Classid&".htm"

FilePath = Server.MapPath("/")&"\"&FileName

Html_Temp = Html_Temp&FilePath&"</LI>"

Do_Url = "http://"

Do_Url = Do_Url&Request.ServerVariables("SERVER_NAME")&"/main/index.asp"

Do_Url = Do_Url&"?Item_Classid="&Item_Classid



strUrl = Do_Url

dim objXmlHttp

set objXmlHttp = Server.createObject("Microsoft.XMLHTTP")

objXmlHttp.open "GET",strUrl,false

objXmlHttp.send()

Dim binFileData

binFileData = objXmlHttp.responseBody

Dim objAdoStream

set objAdoStream = Server.createObject("ADODB.Stream")

objAdoStream.Type = 1

objAdoStream.Open()

objAdoStream.Write(binFileData)

objAdoStream.SaveToFile FilePath,2 

objAdoStream.Close()


Next

Html_Temp = Html_Temp&"<UL>"

%>



<%

Response.Write ( "成功生成文件:" )

Response.Write ( "
" )

Response.Write Html_Temp

%>






<!--#include file="conn.asp"-->

<%

dim str

function menu(id)

set rs=server.CreateObject("adodb.recordset")

sql="select * from menu where id1="&id&" order by url,id"

rs.open sql,conn,1,1

str=str+"<table border='0' cellspacing='0' cellpadding='0'>"+chr(13)

i=1

while not rs.eof

ChildCount=conn.execute("select count(*) from menu where id1="&rs("id"))(0)

if ChildCount=0 then

 if i=rs.recordcount then

  menutype="file1"

 else

  menutype="file"

 end if

 menuname="<a f1=fso.opentextfile(server.MapPath("demo.htm"),2,true)

f1.write("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>")

f1.write("<html>")

f1.write("<head>")

f1.write("<meta http-equiv='Content-Type' content='text/html; charset=gb2312'>")

f1.write("<title>哈哈~~~!这回速度超快了吧?</title>")

f1.write("<meta name='keywords' content='asp2004,树,菜单,无限级'>")

f1.write("<meta name='author' content='小灰,quxiaohui_0163.com'>")

f1.write("<link language='javascript' src='function.js'></script>")

f1.write("</head>")

f1.write("<body>")

f1.write("<a fout = fso.CreateTextFile(server.mappath(""&filename&""))

fout.write request.form("body")

fout.close

set fout=nothing

set fso=nothing

end if

%>

这样index.html文件就生成了,连模板都用不着,只要将正常情况下使用的ASP文件读取到textarea里就可以了,目前尚未发现问题!当然前提是服务器要支持FSO

分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: