ASP. NET static page generation method, asp.net static generation

Source: Internet
Author: User

ASP. NET static page generation method, asp.net static generation

This document describes how to generate ASP. NET static pages. Share it with you for your reference. The specific implementation method is as follows:

I. Problems:

Because of the business needs, You have to generate static pages based on the template pages, so you have to write some ideas for future use.

Ii. solution:

Static Page generation uses matching and replacement most. First, you must read the html content of the template page and then perform tag matching defined by yourself, for example, if I want to replace the title label I defined with the title content of the read database, I can directly read the database title, replace it directly, and generate an html file.

The Code is as follows:
Copy codeThe Code is as follows: // <summary>
/// Parse the matching tags in the html of the template and replace the tags (currently, it can only be used for pages without pagination)
/// </Summary>
/// <Param name = "html"> HTML </param>
/// <Returns> return the replaced HTML </returns>
Public static string ReturnHtml (string html)
{
String newhtml = html;
Newhtml = newhtml. Replace ("<# Title #>", "Replace Title"); // Replace Title
// Newhtml = newhtml. Replace ("<# Content #>", "Replace Content"); // Replace title
Newhtml = CreateList (newhtml );
Return newhtml;
}

/// <Summary>
/// Read the HTML file
/// </Summary>
/// <Param name = "temp"> relative path of the html file </param>
/// <Returns> returns html </returns>
Public static string ReadHtmlFile (string temp)
{
StreamReader sr = null;
String str = "";
Try
{
Sr = new StreamReader (HttpContext. Current. Server. MapPath (temp), code );
Str = sr. ReadToEnd (); // read the file
}
Catch (Exception exp)
{
HttpContext. Current. Response. Write (exp. Message );
HttpContext. Current. Response. End ();
}
Finally
{
Sr. Dispose ();
Sr. Close ();

}
Return str;
}

/// <Summary>
/// Generate an html file
/// </Summary>
/// <Param name = "filmname"> file name (with relative path, for example, ../a.html) </param>
/// <Param name = "html"> html content (entire) </param>
Public static void writeHtml (string filmname, string html)
{
System. Text. Encoding code = System. Text. Encoding. GetEncoding ("UTF-8 ");
String htmlfilename = HttpContext. Current. Server. MapPath (filmname );
String str = html;
StreamWriter sw = null;
// Write an object
Try
{
Sw = new StreamWriter (htmlfilename, false, code );
Sw. Write (str );
Sw. Flush ();
}

Catch (Exception ex)
{
HttpContext. Current. Response. Write (ex. Message );
HttpContext. Current. Response. End ();
}

Finally
{
Sw. Close ();
}
}

From the code, we can see that the process of generating a static page is actually like this: Read the source code of the template page-> match and replace the custom tag with the actual content-> finally generate a new html file, this is the way of thinking. I have never done it before. I think it is too complicated. Now, when I write it on my own, I find it not very complicated.

Finally, if some pages are generated, that is, the list page is generated cyclically. If you do not know how many pages are generated, you can reply to the question, I want to answer your questions as much as possible. Of course, I can't do anything I don't understand. After all, I just got in touch with this function. Now I get the most simple look, the figure tells you a joke:

I hope this article will help you design your asp.net program.

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.