Tuesday, 19 July 2011

Generating static page using ASP.NET 2.0 and VB.NET


This tutorial will show you how to generate static page using ASP.NET 2.0 and VB.

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.

Imports System.IO

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

We use the btn_Generate_Click and two customed functons fo Stream2Stream,GetFileStream to excute the task. The detailed code as following:

Protected Sub btn_Generate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Generate.Click
Dim filename As String = Server.MapPath("staticHtml_1.html")
Dim s As Stream = GetFileStream(filename)
If s IsNot Nothing Then
Using s
Stream2Stream(s, Response.OutputStream)
Response.[End]()
End Using
End If

Dim sw As New StringWriter()
Server.Execute("Main_Execute.aspx", sw)

Dim content As String = sw.ToString()

Response.Write(content)
Response.Flush()

Try
Using fs As New FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write)
Using streamwriter As New StreamWriter(fs, Response.ContentEncoding)
streamwriter.Write(content)
End Using
End Using
Finally
Response.End()
End Try
End Sub

Public Shared Sub Stream2Stream(ByVal src As Stream, ByVal dst As Stream)
Dim buf As Byte() = New Byte(4095) {}
While True
Dim c As Integer = src.Read(buf, 0, buf.Length)
If c = 0 Then
Return
End If
dst.Write(buf, 0, c)
End While
End Sub

Public Function GetFileStream(ByVal filename As String) As Stream
Try
Dim dt As DateTime = File.GetLastWriteTime(filename)
Dim ts As TimeSpan = dt - DateTime.Now
If ts.TotalHours > 1 Then
Return Nothing
End If
Return New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
Catch
Return Nothing
End Try
End Function

The front end GenerateStaticPageVB.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>

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

The flow for the code behind page is as follows

Imports System.IO

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
' 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
End Sub

Protected Sub btn_Generate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Generate.Click
Dim filename As String = Server.MapPath("staticHtml_1.html")
Dim s As Stream = GetFileStream(filename)
If s IsNot Nothing Then
Using s
Stream2Stream(s, Response.OutputStream)
Response.[End]()
End Using
End If

Dim sw As New StringWriter()
Server.Execute("Main_Execute.aspx", sw)

Dim content As String = sw.ToString()

Response.Write(content)
Response.Flush()

Try
Using fs As New FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write)
Using streamwriter As New StreamWriter(fs, Response.ContentEncoding)
streamwriter.Write(content)
End Using
End Using
Finally
Response.End()
End Try
End Sub

Public Shared Sub Stream2Stream(ByVal src As Stream, ByVal dst As Stream)
Dim buf As Byte() = New Byte(4095) {}
While True
Dim c As Integer = src.Read(buf, 0, buf.Length)
If c = 0 Then
Return
End If
dst.Write(buf, 0, c)
End While
End Sub

Public Function GetFileStream(ByVal filename As String) As Stream
Try
Dim dt As DateTime = File.GetLastWriteTime(filename)
Dim ts As TimeSpan = dt - DateTime.Now
If ts.TotalHours > 1 Then
Return Nothing
End If
Return New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)
Catch
Return Nothing
End Try
End Function
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment