ASP. NET generates implementation code for static Web pages

Source: Internet
Author: User
Tags httpcontext

Now do the program to convert the dynamic page into a static page, today teaches you to implement static page generation method in ASP.

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.IO;
Using System.Text;
///
Summary description of WriteFile
///
public class WriteFile
{
Public WriteFile ()
{
}
public static bool Createhtml (string[] strnewshtml,string[] stroldhtml,string strmodefilepath,string strPath)
{
BOOL flag = FALSE;
StreamReader sr = null;
StreamWriter SW = null;
string filepath = HttpContext.Current.Server.MapPath (Strmodefilepath);
Encoding code = encoding.getencoding ("gb2312");
string s = String. Empty;
Try
{
sr = new StreamReader (Filepath,code);
s = Sr. ReadToEnd ();
}
catch (Exception ex)
{
Throw ex;
}
Finally
{
Sr. Close ();
}
Try
{
for (int i = 0; i < strnewshtml.length; i++)
{
s = S.replace (Stroldhtml[i], strnewshtml[i]);
}
SW = new StreamWriter (HttpContext.Current.Server.MapPath (strpath), false, code);
Sw. Write (s);
Flag = true;
}
catch (Exception ex)
{
Flag = false;
Throw ex;
}
Finally
{
Sw. Flush ();
Sw. Close ();
}
return flag;
}
public static bool Updatehtmlpage (string[] strnewshtml, string[] strstarthtml, string[] strendhtml, string strhtml)
{
BOOL Flage = false;
StreamReader readerfile = null;
StreamWriter wrirtefile = null;
String FilePath = HttpContext.Current.Server.MapPath (strhtml);
Encoding Code = encoding.getencoding ("gb2312");
String strfile = String. Empty;
Try
{
Readerfile = new StreamReader (FilePath, Code);
strfile = Readerfile.readtoend ();
}
catch (Exception ex)
{
Throw ex;
}
Finally
{
Readerfile.close ();
}
Try
{
int intlength = Strnewshtml.length;
for (int i = 0; i < intlength; i++)
{
int intstart = Strfile.indexof (Strstarthtml[i]) + strstarthtml[i]. Length;
int intend = Strfile.indexof (strendhtml[i]);
String stroldhtml = Strfile.substring (Intstart, Intend-intstart);
strfile = Strfile.replace (stroldhtml, strnewshtml[i]);
}
Wrirtefile = new StreamWriter (FilePath, False, Code);
Wrirtefile.write (strfile);
Flage = true;
}
catch (Exception ex)
{
Throw ex;
}
Finally
{
Wrirtefile.flush ();
Wrirtefile.close ();
}
return flage;
}
}
Call public class:
----------------------------------------------------------------------------
protected void button2_click (object sender, EventArgs e)
{
String newstitle = this. TextBox1.Text;
String newskindname = this. DropDownList1.SelectedItem.Text;
String newsbody = this. Webeditor1.text;
DateTime pubtime = DateTime.Now;
String UserName = session["UserName"]. ToString ();
Response.Write (Newskindname);
string[] strnewshtml = new string[] {newstitle, newskindname, Newsbody, pubtime.tostring (), UserName};
string[] stroldhtml = new string[] {"@Title", "@NewsKInd", "@NewsBody", "@PubTime", "@UserName"};
String strFileName = DateTime.Now.ToString ("DDHHMMSS") + ". html";
String strFilePath = String. Format ("newshtml/{0}", strFileName);
Try
{
if (writefile.createhtml (strnewshtml, stroldhtml, "mode.htm", strFilePath))
{
This. Label1.Text = "Build succeeded!";
}
Else
{
This. Label1.Text = "Build failed!";
}
}
Catch
{
This. Label1.Text = "Build failed!";
}
}
protected void Button3_Click (object sender, EventArgs e)
{
String[] Strnewshtml=new string[]{"To this trip!"};
String[] strstarthtml=new string[]{""};
String[] strendhtml=new string[]{""};
if (Writefile.updatehtmlpage (strnewshtml, strstarthtml, strendhtml, "newshtml/02011139.html"))
{
This. label1.text= "Generate homepage success!";
}
Else
{
This. Label1.text= "Build home failed!";
}
}

Create new folder newshtml, generate HTML file to put inside

-----------------------------------------------------------

Add a template file

<title> Untitled Page </title>
<body>
<table border= "1" cellpadding= "0" cellspacing= "0" style= "WIDTH:522PX; Height:338px ">
<tr>
&LT;TD align= "center" colspan= "2" >
@Title </td>
</tr>
<tr>
&LT;TD align= "center" colspan= "2" >
Publisher: @UserName &nbsp; &nbsp; Release time: @PubTime &nbsp; &nbsp;&nbsp; News Category: @NewsKInd </td>
</tr>
<tr>
&LT;TD colspan= "2" >
@NewsBody </td></tr><tr>
&LT;TD style= "width:100px" >
&LT;/TD&GT;&LT;TD style= "width:100px" >
</td></tr></table></body>

Reprinted from: http://www.aspnetjia.com

ASP. NET generates implementation code for static Web pages

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.