Asp.net static page generation methods

Source: Internet
Author: User
Tags datetime flush httpcontext

The most common method is to read a template from a file, replace the parameters in the template, and output the file. This method is generated much faster than the first method, and the template content can be edited with tools.
Main code:

Using system;
Using system. collections;
Using system. componentmodel;
Using system. data;
Using system. drawing;
Using system. web;
Using system. web. sessionstate;
Using system. web. ui;
Using system. web. ui. webcontrols;
Using system.web.ui.html controls;
Using system. io;
Using system. text;

Namespace xinxi
{
/// <Summary>
/// Summary of createpage.
/// </Summary>
// Www.111cn.net
// This is a small program that generates static web pages.

Public class create
{
Public void createpage ()
  {
 
  }
Public static bool writefile (string strtext, string strcontent, string strauthor)
  {
String path = httpcontext. current. server. mappath ("/test/"); // file output Directory
Encoding code = encoding. getencoding ("gb2312 ");
// Read the template file
String temp = httpcontext. current. server. mappath ("/template/test.html"); // template file
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"; // static file name

// Replace content
// At this time, the template file has been read into the variable named str
Str = str. replace ("showarticle", strtext); // showarticle on the template page
Str = str. replace ("biaoti", strtext );
Str = str. replace ("content", strcontent );
Str = str. replace ("author", strauthor );
// Write an object
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;
  }
}
}

// The principle is to use the class in system. io to read and write the template file, replace the tag in the template with replace, and write the static html

See the instance below

Template

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

C code

Public static bool writefile (string strtext, string strcontent, string strauthor)
  {
String path = httpcontext. current. server. mappath ("/news /");
Encoding code = encoding. getencoding ("gb2312 ");
// Read the 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 ";
// Replace content
// At this time, the template file has been read into the variable named str
Str = str. replace ("showarticle", strtext); // showarticle on the template page
Str = str. replace ("biaoti", strtext );
Str = str. replace ("content", strcontent );
Str = str. replace ("author", strauthor );
// Write an object
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;
  }

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.