asp.net/asp generate static HTML pages

Source: Internet
Author: User
Tags chr current time html page httpcontext include string tostring root directory
asp.net| Static

Environment: Microsoft. NET Framework SDK v1.1

Os:windows Server 2003 Chinese version

Asp. NET generates static HTML pages

The FileSystemObject object for generating static pages implemented in ASP!

This type of operation is involved in. NET System.IO

The following is the program codeNote: This code is not original! Refer to someone else's code

Generate HTML page

Public static BOOLWriteFile (stringStrText,stringStrcontent,stringStrauthor)

{

stringPath = HttpContext.Current.Server.MapPath ("/news/");

Encoding code = encoding.getencoding ("gb2312");

Reading template files

stringtemp = HttpContext.Current.Server.MapPath ("/news/text.html");

StreamReader sr=NULL;

StreamWriter sw=NULL;

String Str= "";

Try

{

SR =NewStreamReader (temp, code);

str = Sr. ReadToEnd (); Reading files

}

Catch(Exception exp)

{

HttpContext.Current.Response.Write (exp. message);

HttpContext.Current.Response.End ();

Sr. Close ();

}


stringHtmlfilename=datetime.now.tostring ("YYYYMMDDHHMMSS") + ". html";

Replace content

At this point, the template file has been read into a variable named str.

Str =str. Replace ("Showarticle", StrText); Showarticle in the file://template page

str = str. Replace ("Biaoti", StrText);

str = str. Replace ("Content", strcontent);

str = str. Replace ("Author", Strauthor);

Write a file

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;



This function is placed in the Conn.CS base class.

References  

    if (Hover.Conn.WriteFilethis.Title.Text.ToString), this. Content.Text.ToString), this. Author.Text.ToString)))

    {

     response.write (" Add Success ");

    

    else

    {

     response.write ("Generate HTML Error!");

    

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

Template page text.html code

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

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >

<HTML>

<HEAD>

<title>ShowArticle</title>



<body>

Biaoti

<br>

Content<br>

Author

</body>

</HTML>

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

Tip Add a successful HTML file with the current time as the file name! It's just a few of the parameters passed in directly to the HTML file, in the actual application need to add a database, and then write HTML file

And you need to write the generated file name and so on, and so on, this instance is only implemented based on the submission of parameters to replace the corresponding fields in the template! Need to perfect a lot of places! Who has the opinion, welcome to enlighten!



Asp. NET generates static HTML pages! 2004-06-01 13:36 Kriss

I found the way I wanted to use it:

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));

}


So I can create an HTML file directly from an ASPX file.

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

Host. DoRequest ("default.aspx", string. EMPTY,FS);

How to generate static Web pages by ASP


With the increase in the number of visits to the Web site, each read from the database is the cost of efficiency, many use Access as a database will be more experience, static page add in search, will be given priority. The popular practice on the internet is to write the data source code into the database and then read from the database to generate static surface, so that the invisible room to enlarge the database. Creating a static page directly from an existing ASP page will save you a lot of money.


The following example is the three dynamic pages that will, index.asp?id=1/index.asp?id=2/index.asp?id=3/, respectively, generate the ndex1.htm,index2.htm,index3.htm existence root directory below:


<%

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 ("Successfully generated file:")

Response.Write ("<BR>")

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 href= '" &rs ("url") & "' target= ' _blank ' >" &rs ("Menuname") & "</a>"

Onmouseup= ""

Else

If I=rs.recordcount Then

Menutype= "Menu3"

Listtype= "List1"

&AMP;RS ("id") & "', ' B" &rs ("id") & "');"

Else

Menutype= "Menu1"

Listtype= "List"

&AMP;RS ("id") & "', ' B" &rs ("id") & "');"

End If

Menuname=rs ("Menuname")

End If

str=str+ "<tr><td id= ' B" &rs ("id") & "' class= '" &menutype& "'" &onmouseup& ">" & menuname& "</td></tr>" +CHR (13)

If Childcount>0 Then

str=str+ "<tr id= ' a" &rs ("id") & "' style= ' display:none ' ><td class= '" "&listtype&" ' > "+CHR ( 13)

Menu (RS ("id"))

str=str+ "</td></tr>" +CHR (13)

End If

Rs.movenext

I=i+1

Wend

str=str+ "</table>" +CHR (13)

Rs.close

Set rs=nothing

End Function

Menu (0)

Set Fso=server. CreateObject ("Scripting.FileSystemObject")

Set 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 ("
F1.write ("
F1.write ("<meta http-equiv= ' content-type ' content= ' text/html;") charset=gb2312 ' > ')

F1.write ("<title> haha ~~~! Is this going to be too fast? </title> ")

F1.write ("<meta name= ' keywords ' content= ' asp2004, tree, menu, Infinity ' >")

F1.write ("<meta name= ' author ' content= ' small ash, quxiaohui_0@163.com ' >")

F1.write ("<link href= ' style.css ' rel= ' stylesheet ' type= '" Text/css ' > ")

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

F1.write ("
F1.write ("<body>")

F1.write ("<a href= ' http://asp2004.net ' target= ' _blank ' >http://asp2004.net</a> offers the latest downloads <a href= ' Menu.asp ' > Management </a> <br> ')

F1.write (str)

F1.write ("</body>")

F1.write ("
F1.close

Set fso=nothing

Response. Redirect ("demo.htm")

%>


One way to generate static HTML pages using only ASP+FSO, without templates (especially for content-intensive pages)

FSO to generate static HTML files when the replacement template label has been a very troublesome problem, at least I think so, but also to do a template, trouble! , I saw a way to solve the problem today.

such as a normal index.asp page, and the ASP code to bring up the contents of the database, and build a makehtml.asp page, add a textarea domain, assuming that name= "body", Index.asp in the textarea, such as:

<textarea name= "Body" ><!--#include file= "index.asp"--></textarea>, include this textarea in the form, Create an FSO object in the Receive form page, such as index.html file generation!

<%

Filename= ". /index.html "

If request ("Body") <> "" Then

Set fso = Server.CreateObject ("Scripting.FileSystemObject")

Set fout = FSO. CreateTextFile (Server.MapPath ("&filename&"))

Fout.write Request.Form ("Body")

Fout.close

Set fout=nothing

Set fso=nothing

End If

%>

So index.html file is generated, even templates are not needed, as long as the normal use of the ASP file read into the textarea can be, at present have not found the problem! Of course, the premise is the server to support FSO



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.