Five scenarios for how to generate static pages

Source: Internet
Author: User
Tags format define array flush html page httpcontext net xml tostring
static | page

  Programme 1:
<summary>
Incoming URL returns the HTML code for a Web page
</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 a 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 of the client of the Web page, and then save it to the. html file.

  Programme 2:

It is not difficult to generate a single static page, but it is difficult to keep the relationship and links between static pages intact, especially when the pages are frequently updated, modified, or deleted;

Like Alibaba's page is also all HTML, estimated using the address mapping function about address mapping can refer to: http://www.easewe.com/Article/ShowArticle.aspx?article=131

Take a look at this page and analyze his "Bid Countdown" feature http://info.china.alibaba.com/news/subject/v1-s5011580.html?head=top4&Bidding= Home5

  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
Here is the program code Note: This code is not original! Refer to other people's Code

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");
Reading template files
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 (); Reading files
}
catch (Exception exp)
{
HttpContext.Current.Response.Write (exp. message);
HttpContext.Current.Response.End ();
Sr. Close ();
}


String htmlfilename=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 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 to reference the note in the code to add News: project name is hover

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

Scenario 3: Example of a client reference (SJ)

Its function is to obtain the code of a page in the way of the client, and then it can be used for other purposes, this example is the direct output

<script>
var oxmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
Oxmlhttp.open ("Get", "http://www.webjx.com", false);
Oxmlhttp.send ()
var ostream = new ActiveXObject ("ADODB. Stream ");
if (ostream = null)
Alert ("Your machine does not support 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>

  Scenario 4: Learn csdn. Data is saved in XML, and template XSL has only one file.

Use XML to save data, use XSL to define templates, and to live with data. You can use XSL to easily display data in the client or service segment. It's easier if you want to generate a static leaf. Check out the. NET XML class pack problem.

Advantages: Easy to quickly convert to the format and content you want.
Disadvantages: Need to learn more content, not easy to get started.

Programme 5:

Ideas

1. Use tools such as DW-MX to generate HTML-formatted templates, add special tags (such as $htmlformat$) where needed, and dynamically generate files using code to read the template, and then get the foreground input to add to the tag location of the template, Writes the new file name to the disk and writes the relevant data to the database after it is written.
2. Using background code to hard-code HTML files, you can use the HtmlTextWriter class to write HTML files.
Advantages

1. Can be a very complex page, using the method containing JS files, in the JS file to add document.write () method can be added to all pages, such as page header, advertising and other content.

2. Static HTML files Use MS Windows2000 's Index server to create Full-text search engines, using asp.net to get search results in a DataTable. The Win2000 Index Service was unable to find the contents of the XML file. If you include a database search and index index double lookup, then this search function will be very powerful.

3. Save the load on the server and request a static HTML file to save a lot more than an ASPX file server resource.

Disadvantages

Idea two: If the hard coded way, the workload is very large, need a lot of HTML code. Debugging difficult. And the HTML style generated with hard coding cannot be modified, and if the site is replaced, it must be coded to bring a huge workload to the later stage.

So here's the first way to take it.

Column Code

1. Define (template.htm) HTML Template page

$htmlformat [3]

2.asp.net Code:

---------------------read the HTML template page to the StringBuilder object----

String[] Format=new string[4];//defines an array with a consistent number of Htmlyem tags

StringBuilder htmltext=new StringBuilder ();

Try

{

using (StreamReader sr = new StreamReader ("path and page name for Template page"))

{

String Line;

while (line = Sr. ReadLine ())!= null)

{

HTMLText. Append (line);

}

Sr. Close ();

}

}

Catch

{

Response.Write ("");

}

---------------------assign a value to an array of tokens------------

format[0]= "background=" bg.jpg "";//Background picture

Format[1]= "#990099";//Font Color

Format[2]= "150px";//font size

format[3]= " generated template HTML page ";//Text description

----------Replace the tags in htm for 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 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 a circular replacement, so the template that needs to replace a large number of elements is fast.



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.