Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
ASP.NET: Use Response.Filter to intercept your HTML
From Wiki
Summary: You can use Response.Filter to get a reference to the final HTML that will be sent to the client
Sometimes, we need to see the actual HTML that is going to be sent to our client once a page is requested. There are many uses for this and to demonstrate it's functionality, I'm going to show you how the Response.Filter class can be used to intercept the HTML and change some text before it is sent to the client.
First, let's create a page named "MyPage.aspx" and add the following:
- <%@ Page Language="VB" AutoEventWireup="false" CodeFile="MyPage.aspx.vb" Inherits="MyPage" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>Untitled Page</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- Hello World!
- </div>
- </form>
- </body>
- </html>
If we run this page, we see our simple page displayed and the "Hello World!" message is displayed.
To intercept and get a reference to the HTML, we now need to create a class to inherit System.IO.Stream. So, create a new class in your App_Code folder, call it "ReplaceHTML" and add the following code:
- Imports Microsoft.VisualBasic
- Public Class ReplaceHTML
- Inherits System.IO.Stream
- Private Base As System.IO.Stream
- Public Sub New(ByVal ResponseStream As System.IO.Stream)
- If ResponseStream Is Nothing Then Throw New ArgumentNullException("ResponseStream")
- Me.Base = ResponseStream
- End Sub
- Public Overrides ReadOnly Property CanRead() As Boolean
- Get
- End Get
- End Property
- Public Overrides ReadOnly Property CanSeek() As Boolean
- Get
- End Get
- End Property
- Public Overrides ReadOnly Property CanWrite() As Boolean
- Get
- End Get
- End Property
- Public Overrides Sub Flush()
- End Sub
- Public Overrides ReadOnly Property Length() As Long
- Get
- End Get
- End Property
- Public Overrides Property Position() As Long
- Get
- End Get
- Set(ByVal value As Long)
- End Set
- End Property
- Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
- Return Me.Base.Read(buffer, offset, count)
- End Function
- Public Overrides Function Seek(ByVal offset As Long, ByVal origin As System.IO.SeekOrigin) As Long
- End Function
- Public Overrides Sub SetLength(ByVal value As Long)
- End Sub
- Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)
- ' Get HTML code
- Dim HTML As String = System.Text.Encoding.UTF8.GetString(buffer, offset, count)
- ' Replace the text with something else
- HTML = HTML.Replace("Hello World!", "I've replaced the Hello World example!")
- ' Send output
- buffer = System.Text.Encoding.UTF8.GetBytes(HTML)
- Me.Base.Write(buffer, 0, buffer.Length)
- End Sub
- End Class
You can see from the above code that when the class is created, the New method sets it's "Base" variable to the ResponseStream that we will soon send in from the page. The Write method then performs a Replace on the actual HTML and then we write it back out.
All we need to do now is tell the page to call this class when the page loads. This is where the Response.Filter method comes in and it's very easy to implement. Simply add this code to the "MyPage.aspx" page that we created earlier:
- Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- Response.Filter = New ReplaceHTML(Response.Filter)
- End Sub
Now, when we run the page, our "Hello World!" text has been replaced just as we asked it to!
Obviously this is just a simple example, but this could easily be extended to incorporate Regular Expressions if anything more complicated is needed, but hopefully this provides a starting point.
This Hack is part of the ASP.NET Hacks collection



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.