DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Generating static page using ASP.NET 2.0 and C#

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;

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;
}
}

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 front end GenerateStaticPageCsharp.aspx page looks something like this:

<div align="left" style="text-align: center">
&nbsp;<table>
<tr>
<td colspan="2" style="width: 402px">
&nbsp;&nbsp;</td>
</tr>
<tr>
<td colspan="2" style="height: 26px; width: 402px;">
<asp:Button ID="btn_Generate" runat="server" Text="Generate" OnClick="btn_Generate_Click" /></td>
</tr>
</table>
&nbsp;&nbsp;
</div>

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.DotNetTutorials.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;
}
}
}

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Looking for more ASP.NET Tutorials? Click Here!