Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

ASP.NET: Use Response.Filter to intercept your HTML

From Wiki

Jump to: navigation, search

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:

  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="MyPage.aspx.vb" Inherits="MyPage" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.  
  4. <html xmlns="http://www.w3.org/1999/xhtml" >  
  5. <head runat="server">  
  6.     <title>Untitled Page</title>  
  7. </head>  
  8. <body>  
  9.     <form id="form1" runat="server">  
  10.     <div>  
  11.         Hello World!  
  12.     </div>  
  13.     </form>  
  14. </body>  
  15. </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:

  1. Imports Microsoft.VisualBasic  
  2.  
  3. Public Class ReplaceHTML
  4.     Inherits System.IO.Stream
  5.  
  6.     Private Base As System.IO.Stream
  7.  
  8.     Public Sub New(ByVal ResponseStream As System.IO.Stream)
  9.         If ResponseStream Is Nothing Then Throw New ArgumentNullException("ResponseStream")
  10.         Me.Base = ResponseStream
  11.     End Sub
  12.  
  13.     Public Overrides ReadOnly Property CanRead() As Boolean
  14.         Get
  15.  
  16.         End Get
  17.     End Property
  18.  
  19.     Public Overrides ReadOnly Property CanSeek() As Boolean
  20.         Get
  21.  
  22.         End Get
  23.     End Property
  24.  
  25.     Public Overrides ReadOnly Property CanWrite() As Boolean
  26.         Get
  27.  
  28.         End Get
  29.     End Property
  30.  
  31.     Public Overrides Sub Flush()
  32.  
  33.     End Sub
  34.  
  35.     Public Overrides ReadOnly Property Length() As Long
  36.         Get
  37.  
  38.         End Get
  39.     End Property
  40.  
  41.     Public Overrides Property Position() As Long
  42.         Get
  43.  
  44.         End Get
  45.         Set(ByVal value As Long)
  46.  
  47.         End Set
  48.     End Property
  49.  
  50.     Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
  51.         Return Me.Base.Read(buffer, offset, count)
  52.     End Function
  53.  
  54.     Public Overrides Function Seek(ByVal offset As Long, ByVal origin As System.IO.SeekOrigin) As Long
  55.  
  56.     End Function
  57.  
  58.     Public Overrides Sub SetLength(ByVal value As Long)
  59.  
  60.     End Sub
  61.  
  62.     Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)
  63.         ' Get HTML code  
  64.         Dim HTML As String = System.Text.Encoding.UTF8.GetString(buffer, offset, count)
  65.  
  66.         ' Replace the text with something else  
  67.         HTML = HTML.Replace("Hello World!", "I've replaced the Hello World example!")
  68.  
  69.         ' Send output  
  70.         buffer = System.Text.Encoding.UTF8.GetBytes(HTML)
  71.         Me.Base.Write(buffer, 0, buffer.Length)
  72.     End Sub
  73.  
  74. 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:

  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.     Response.Filter = New ReplaceHTML(Response.Filter)
  3. 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

461 Rating: 3.0/5 (7 votes cast)