Asp. NET Static page Generation method _ Practical Tips

Source: Internet
Author: User

This example describes the ASP.net static page generation method. Share to everyone for your reference. The implementation methods are as follows:

First, the question:

Because the business needs, the page should be generated according to the template page static page, so I think of the next, write some ideas for future needs when used.

Second, the solution:

Static page generation to use the most is matching and replacement, the first to read the template page HTML content, and then make your own definition of the tag matching, for example, I would like to change my definition of the title tag to read the database header content, you can directly read the title of the database, and then directly replace, Then the HTML file is generated and OK.

The specific code is as follows:

Copy Code code as follows:
<summary>
Parse the matching label in the HTML of the template and replace it (temporarily only for pages without pagination)
</summary>
<param name= "HTML" >HTML</param>
<returns> return to html</returns> after replacement
public static string returnhtml (string html)
{
string newhtml = html;
newhtml = newhtml. Replace ("< #Title #>", "This is title replacement");/replace title
newhtml = newhtml. Replace ("< #Content #>", "This is content substitution");/replace title
newhtml = CreateList (newhtml);
return newhtml;
}

<summary>
Reading HTML files
</summary>
<param name= The relative path of the "temp" >html file </param>
<returns> return to 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 (); Reading files
}
catch (Exception exp)
{
HttpContext.Current.Response.Write (exp. message);
HttpContext.Current.Response.End ();
}
Finally
{
Sr. Dispose ();
Sr. Close ();

}
return str;
}

<summary>
Generate HTML file
</summary>
<param name= "Filmname" > filename (with relative path path, such as:.. /a.html) </param>
<param name= "HTML" >html content (whole) </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 a file
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 ();
}
}

It can be seen from the code, generate a static page is actually a process: Read the template page of the source-> matching replace the custom label for the actual content-> finally regenerated into a new HTML file, thinking so go, had not done before, feel too complicated, and now actively write, It's not that complicated.

And finally, if there's a list of some raw pages, that is, the list of pages to be generated, how many pages to generate a number of static page files, if you do not understand the answer to ask, I understand that I try to answer for everyone, of course, do not understand I can do nothing, after all, I have just touched this function, Now for the moment to make a most humble appearance, drawings come up to everyone joke jokes:

I hope this article will help you with the ASP.net program design.

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.