Using Response.Filter

Last post 01-19-2004 10:50 PM by iwilld0it. 1 replies.

Sort Posts:

  • Using Response.Filter

    01-19-2004, 1:54 PM
    • Member
      32 point Member
    • iwilld0it
    • Member since 09-18-2002, 6:57 AM
    • Posts 7
    For those of u interested in using the Response.Filter property. Here is a custom filter class I wrote ...


    Public Class ResponseFilter
    Inherits IO.Stream

    Private mFilter As IO.Stream
    Private mEncoding As System.Text.Encoding

    Sub New()
    With HttpContext.Current.Response
    mFilter = .Filter
    mEncoding = .ContentEncoding
    End With
    End Sub

    Public Overrides ReadOnly Property CanRead() As Boolean
    Get
    Return mFilter.CanRead
    End Get
    End Property

    Public Overrides ReadOnly Property CanSeek() As Boolean
    Get
    Return mFilter.CanSeek
    End Get
    End Property

    Public Overrides ReadOnly Property CanWrite() As Boolean
    Get
    Return mFilter.CanWrite
    End Get
    End Property

    Public Overrides Sub Flush()
    Call mFilter.Flush()
    End Sub

    Public Overrides ReadOnly Property Length() As Long
    Get
    Return mFilter.Length
    End Get
    End Property

    Public Overrides Property Position() As Long
    Get
    Return mFilter.Position
    End Get
    Set(ByVal Value As Long)
    mFilter.Position = Value
    End Set
    End Property

    Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer) As Integer
    If mFilter.CanRead Then
    Return mFilter.Read(buffer, offset, count)
    Else
    Return -1
    End If
    End Function

    Public Overrides Function Seek(ByVal offset As Long, ByVal origin As System.IO.SeekOrigin) As Long
    If mFilter.CanSeek Then
    Return mFilter.Seek(offset, origin)
    Else
    Return -1
    End If
    End Function

    Public Overrides Sub SetLength(ByVal value As Long)
    Call mFilter.SetLength(value)
    End Sub

    Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)
    ' Place Response Text In A String Builder
    Dim output As New StringBuilder(mEncoding.GetString(buffer, offset, count))

    ' Perform Global Replaces
    output = output.Replace("{HOST}", "http://www.mysite.com")

    ' Write The Newly Modified Response Stream
    mFilter.Write(mEncoding.GetBytes(output.ToString), 0, output.Length)
    End Sub
    End Class


    You can call Response.Filter in a global handler in global.asax like so ...


    Private Sub Global_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.PreRequestHandlerExecute

    Response.Filter = New ResponseFilter()

    End Sub


    Create a test page and see of it works ...

    MyTest.aspx
    --------------


    <html>
    <body>
    My Page
    </body>
    </html>


    The Filtered HTML will translate to ...


    <html>
    <body>
    <a href="http://www.mysite.com/MyFolder/default.aspx">;My Page</a >
    </body>
    </html>

  • Re: Using Response.Filter

    01-19-2004, 10:24 PM
    • Member
      32 point Member
    • iwilld0it
    • Member since 09-18-2002, 6:57 AM
    • Posts 7
    I made a slight inefficiency within the global replace ... here is the ajusted portion of the code ...


    Public Overrides Sub Write(ByVal buffer() As Byte, ByVal offset As Integer, ByVal count As Integer)

    ' Place Response Text In A String Builder

    Dim output As New StringBuilder(mEncoding.GetString(buffer, offset, count))

    ' HERE IS THE ADJUSTMENT: Perform Global Replaces
    Call output.Replace("{HOST}", "http://www.mysite.com")

    ' Write The Newly Modified Response Stream

    mFilter.Write(mEncoding.GetBytes(output.ToString), 0, output.Length)

    End Sub

    End Class

Page 1 of 1 (2 items)