locked
Using Response.Filter RRS feed

  • Question

  • User432095116 posted
    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 -------------- My Page The Filtered HTML will translate to ... ;My Page
    Monday, January 19, 2004 1:54 PM

All replies

  • User432095116 posted
    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
    Monday, January 19, 2004 10:24 PM
  • User-977291873 posted

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

     

    should be

     

    Dim data() As Byte = mEncoding.GetBytes(output.ToString)
    mFilter.Write(data, 0, data.Length)
    
    Sunday, March 18, 2012 11:01 PM
  • User-483024600 posted

    When a browser requests an ASP.NET page from a web server, the ASP.NET engine takes that request through a number of steps that, together, generate the resulting markup, which is returned to the requesting browser for display. The stages in this process are referred to as the HTTP Pipeline and perform tasks like authentication, authorization, and having the requested page render its content. During one of the later stages in the HTTP Pipeline the rendered markup is handed off to a response filter which, if supplied, has an opportunity to inspect and modify the markup before it is returned to the requesting browser.

    Friday, March 23, 2012 7:56 AM