Do Web sites want to use dynamic information, and the dynamic and slow, then come out of the dynamic generation of static methods, ASP generated HTML, but found that the generated content is not the latest, such as I generated the home page, after the generation I have modified the dynamic home page file, in the click Generated, can be generated from the homepage is not a dynamic home page That is, browsing dynamic home page and static home page content is not the same, so here is not the problem of your program, is the cache in mischief, some people said, plus code to prohibit caching, then also can, but the code is more points. Let's learn it. This paper introduces a simple method to solve ASP generated HTML caching problem.
Use the FSO to read the file text box to generate HTML This method is not recommended, you can use fso+xmlhttp generation, with XMLHTTP to obtain the contents of the specified file, with the FSO generated, is very convenient, then solve the cache in this acquisition, is generally an absolute URL, After this URL, add a random number variable. such as default.asp?a= "&now () &", because this is the current time, and the current time does not repeat, let the program think is not a file, there will be no cache to appear, but also to ensure that each generated HTML is the latest.
The following uses XMLHTTP to get dynamic content, and then use the FSO to generate the file.
ASP generates HTML code:
| The code is as follows |
Copy Code |
|
Dim Read,reada,curl,curla,content,contenta
Function gethttppage (URL)
Dim http
Set Http=server.createobject ("Microsoft.XMLHTTP")
Http.open "Get", Url,false
Http.send ()
If Http.readystate<>4 Then
Exit function
End If
Gethttppage=bytestobstr (Http.responsebody, "GB2312")
On Error Resume Next
If Err.number<>0 then err. Clear
End Function
Function Bytestobstr (Body,cset)
Dim objstream
Set objstream = Server.CreateObject ("ADODB.stream")
Objstream. Type = 1
Objstream. Mode =3
Objstream. Open
Objstream. Write body
Objstream. Position = 0
Objstream. Type = 2
Objstream. Charset = Cset
Bytestobstr = objstream. ReadText
Objstream. Close
Set objstream = Nothing
End Function
Url1=request.servervariables ("SERVER_NAME")
Curl= "http://" &url1& "/index.asp?a=" &now () & ""
Read=gethttppage (Curl)
If read<> "" Then
Content=read
Set Fso = Server.CreateObject ("Scripting.FileSystemObject")
Filen=server.mappath ("index.html")
Set Site_config=fso. CreateTextFile (Filen,true, False)
Site_config.write Content
Site_config.close
Set Fso = Nothing
Response.Write ("Already generated home page")
End If
|
The above code generates home page HTML for ASP, or a single file HTML.
Code Explanation:
URL1 is to get the domain name of the current address bar, such as local browsing time for HTTP://127.0.0.1/then he is 127.0.0.1
Curl is to get out of the domain name plus your dynamic file address, here for the index.asp behind the Add a A=now () is to eliminate the cache settings.
Filen is generated to why file, here is index.html, this support relative path, such as the HTML that is born into the previous directory, so write = ". /index.html "
Next time in the publication of ASP generated list and HTML, please pay attention to
examples, replacement methods
such as a normal index.asp dynamic page,
Create a new file makeasp2html.asp
| code is as follows |
copy code |
|
<form Method= "POST" action= "" >
<textarea name= "asp2html" style= "Display:none" ><! ---#include file= "index.asp"--></textarea>
<input type= "Submit" value= " Generate HTML page "/>
</form>
<%
If request. Form ("asp2html") <> "" then
filename= "index.html" &NBSP;
Set fso = Server.CreateObject ("Scripting.FileSystemObject")
Set fout = FSO. CreateTextFile (Server.MapPath ("&filename&"))
fout.write Request.Form (" Asp2html ")
fout.close
set fout=nothing
set fso=nothing
End if
%> |
So index.html files are generated, even templates are not needed, as long as the server to support the FSO, the normal use of the ASP file read into the textarea can be.