Five scenarios for generating HTML static pages using the ASP.NET template

  • 2020-05-12 02:31:54
  • OfStack

ASP.NET template generates HTML static page schema 1:
 
/// < summary> 
///  The incoming URL Back to the web html code  
/// < /summary> 
/// < param name="Url">URL< /param> 
/// < returns>< /returns> 
public static string getUrltoHtml(string Url) 
{ 
errorMsg = ""; 
try 
{ 
System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url); 
// Get the response instance. 
System.Net.WebResponse wResp =wReq.GetResponse(); 
// Read an HTTP-specific property 
//if (wResp.GetType() ==HttpWebResponse) 
//{ 
//DateTime updated =((System.Net.HttpWebResponse)wResp).LastModified; 
//} 
// Get the response stream. 
System.IO.Stream respStream = wResp.GetResponseStream(); 
// Dim reader As StreamReader = New StreamReader(respStream) 
System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312")); 
return reader.ReadToEnd(); 
} 
catch(System.Exception ex) 
{ 
errorMsg = ex.Message ; 
} 
return ""; 
} 

You can use this function to get the html code for the client side of a web page and save it to a.html file.
ASP. NET template generates HTML static page scenario 2:
Generating a single static page is not the hard part. The hard part is how to keep the associations and links between the static pages intact.
Especially if the page is frequently updated, modified, or deleted;
Like alibaba's pages are all html, estimated to use the address mapping function
Take a look at this page and examine its "bid countdown" feature
http://info.china.alibaba.com/news/subject/v1-s5011580.html?head=top4&Bidding=home5
ASP.Net generates the static HTML page
FileSystemObject objects for generating static pages implemented in Asp!
In.Net, this type of operation is referred to as System.IO
The following is the program code note: this code is not original! Reference someone else's code
 
// generate HTML page  
public static bool WriteFile(string strText,string strContent,string strAuthor) 
{ 
string path = HttpContext.Current.Server.MapPath("/news/"); 
Encoding code = Encoding.GetEncoding("gb2312"); 
//  Read template file  
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(); //  Read the file  
} 
catch(Exception exp) 
{ 
HttpContext.Current.Response.Write(exp.Message); 
HttpContext.Current.Response.End(); 
sr.Close(); 
} 

string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html"; 
//  Alternative content  
//  At this moment , The template file has been read into the name str I'm in the variable theta  
str =str.Replace("ShowArticle",strText); // In the template page ShowArticle 
str = str.Replace("biaoti",strText); 
str = str.Replace("content",strContent); 
str = str.Replace("author",strAuthor); 
//  Write files  
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
In the code to add the news reference note: the project is called Hover
 
if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString))) 
{ 
Response.Write(" Add a 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> 
biaoti 
< br> 
content< br> 
author 
< /body> 
< /HTML> 

The html file with the current time as the file name will appear after the prompt is added successfully! Above, we just wrote some parameters directly to the HTML file. In practice, we need to add the database first and then write to the HTML file
ASP. NET template generates HTML static page scenario 3:
Give an example of a client reference (JS)
What it does is take the code for a page in a client-side fashion and use it for other purposes, in this case direct output
 
< script> 
var oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
oXmlHttp.open("GET","http://www.163.com", false); 
oXmlHttp.send() 
var oStream = new ActiveXObject("ADODB.Stream"); 
if(oStream == null) 
alert(" Your machine does not support it ADODB.Stream.") 
else 
{ 
oStream.Type=1; 
oStream.Mode=3; 
oStream.Open() ; 
oStream.Write(oXmlHttp.responseBody); 
oStream.Position= 0; 
oStream.Type= 2; 
oStream.Charset="gb2312"; 
var result= oStream.ReadText(); 
oStream.Close(); 
oStream = null; 
var aa = window.open("","") 
document.write(result); 
aa.document.write(result); 
} 
< /script> 

ASP. NET template generates HTML static page scheme 4:
Learn csdn1 sample. With xml to save data, the template XSL has only one file.
xml is used to save data, xsl is used to define templates and generate data. You can easily display data on the client or server via xsl. It's easier to generate static leaves. Go check 1.net xml class package problem solved.
Pros: easy and fast conversion to the format and content you want.
Disadvantages: need to learn more content, not easy to get started.
ASP. NET template generates HTML static page scheme 5:
Train of thought
1. The use of tools to generate such as Dw - Mx html format templates, in the place where you need to add the format to join special tags (such as $htmlformat $), dynamically generated file using the code read this template, and then obtain the content of the input at the front desk, added to this template tag location, generate new file to disk, write and then write the relevant data into the database.
2. Hard code the Html files with background code, and you can use the HtmlTextWriter class to write the html files.
advantages
1. You can create very complex pages by adding document.write () into the js file using the method containing the js file. You can add content such as page headers, advertisements and so on to all the pages.
2. The static html file can be used to build a full-text search engine with Index Server of MS Windows2000, and asp.net can be used to obtain search results with DataTable. The Index service of Win2000 could not find the contents of the xml file. This search capability is very powerful if you include both database search and Index index double lookup.
3. Save the load on the server, requesting 1 static html file saves a lot of resources compared with 1 aspx file server.
disadvantages
Train of thought 2: if you use hard coding method, the workload is very large, requiring a lot of html code. Debugging is difficult. Moreover, the hard-coded html style cannot be modified. If the website changes the style, it will have to be recoded, which will bring huge workload to the later stage.
So this is the first way of thinking
The code shown in column
1. Define (template.htm)html template page
 
<html> 
<head> 
<title></title> 
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
</head> 
<body > 
<table $htmlformat[0] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px solid #000000"> 
<tr> 
<td width="100%" valign="middle" align="left"> 
<span style="color: $htmlformat[1];font-size: $htmlformat[2]">$htmlformat[3]</span> 
</td> 
</tr> 
</table> 
</body> 
</html> 

2. asp. net code:
 
//--------------------- read html Template page to stringbuilder In the object ---- 
string[] format=new string[4];// Define and htmlyem Tag number 1 To an array of  
StringBuilder htmltext=new StringBuilder(); 
try 
{ 
using (StreamReader sr = new StreamReader(" The path and the page name of the template page ")) 
{ 
String line; 
while ((line = sr.ReadLine()) != null) 
{ 
htmltext.Append(line); 
} 
sr.Close(); 
} 
} 
catch 
{ 
Response.Write("<Script>alert(' Error reading file ')</Script>"); 
} 
//--------------------- Assign a value to the tag array ------------ 
format[0]="background="bg.jpg"";// The background image  
format[1]= "#990099";// The font color  
format[2]="150px";// The font size  
format[3]= "<marquee> Generated templates html page </marquee>";// captions  
//---------- replace htm The mark in is what you want to add  
for(int i=0;i<4;i++) 
{ 
htmltext.Replace("$htmlformat["+i+"]",format[i]); 
} 
//---------- generate htm file ------------------ � �  
try 
{ 
using(StreamWriter sw=new StreamWriter(" Store the path and page name ",false,System.Text.Encoding.GetEncoding("GB2312"))) 
{ 
sw.WriteLine(htmltext); 
sw.Flush(); 
sw.Close(); 
} 
} 
catch 
{ 
Response.Write ("The file could not be wirte:"); 
} 

summary
This method makes it easy to generate html files. The program USES cyclic substitution, so it is very fast for templates that need to replace a large number of elements.

Related articles: