This tutorial will show you how to generate static page using ASP.NET 2.0 and C#.
At first, you will need to import the namespace from System.IO. The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support. Class StringWriter which implements a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder. Class FileStream which exposes a Stream around a file, supporting both synchronous and asynchronous read and write operations. Class StreamWriter which implements a TextWriter for writing characters to a stream in a particular encoding.
| using System.IO; |
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
We use the btn_Generate_Click and two customed functons fo Stream2Stream,GetFileStream to excute the task. The detailed code as following:| protected void btn_Generate_Click(object sender, EventArgs e) { string filename = Server.MapPath("staticHtml_1.html"); }Stream s = GetFileStream(filename); if (s != null) { using (s) }{ Stream2Stream(s, Response.OutputStream); }Response.End(); StringWriter sw = new StringWriter(); Server.Execute("Main_Execute.aspx", sw); string content = sw.ToString(); Response.Write(content); Response.Flush(); try { using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write)) }{ using (StreamWriter streamwriter = new StreamWriter(fs, Response.ContentEncoding)) }{ streamwriter.Write(content); }finally { Response.End (); }static public void Stream2Stream(Stream src, Stream dst) { byte[] buf = new byte[4096]; }while (true) { int c = src.Read(buf, 0, buf.Length); }if (c == 0) return; dst.Write(buf, 0, c);public Stream GetFileStream(string filename) { try }{ DateTime dt = File.GetLastWriteTime(filename); }TimeSpan ts = dt - DateTime.Now; if (ts.TotalHours > 1) return null; return new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read); catch { return null; } |
The front end GenerateStaticPageCsharp.aspx page looks something like this:
| <div align="left" style="text-align: center"> <table> <tr> </table><td colspan="2" style="width: 402px"> </tr> </td> <tr> <td colspan="2" style="height: 26px; width: 402px;"> </tr><asp:Button ID="btn_Generate" runat="server" Text="Generate" OnClick="btn_Generate_Click" /></td> </div> |
Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.
The flow for the code behind page is as follows| 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; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) } { // This tutorial is provided in part by Server Intellect Web Hosting Solutions http://www.serverintellect.com }// Visit http://www.AspNetTutorials.com for more ASP.NET Tutorials protected void btn_Generate_Click(object sender, EventArgs e) { string filename = Server.MapPath("staticHtml_1.html"); }Stream s = GetFileStream(filename); if (s != null) { using (s) }{ Stream2Stream(s, Response.OutputStream); }Response.End(); StringWriter sw = new StringWriter(); Server.Execute("Main_Execute.aspx", sw); string content = sw.ToString(); Response.Write(content); Response.Flush(); try { using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write)) }{ using (StreamWriter streamwriter = new StreamWriter(fs, Response.ContentEncoding)) }{ streamwriter.Write(content); }finally { Response.End (); }static public void Stream2Stream(Stream src, Stream dst) { byte[] buf = new byte[4096]; }while (true) { int c = src.Read(buf, 0, buf.Length); }if (c == 0) return; dst.Write(buf, 0, c); public Stream GetFileStream(string filename) { try }{ DateTime dt = File.GetLastWriteTime(filename); }TimeSpan ts = dt - DateTime.Now; if (ts.TotalHours > 1) return null; return new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);catch { return null; } |
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!
No comments:
Post a Comment