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 PageThe Filtered HTML will translate to ...
;My Page
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
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.
None
0 Points
7 Posts
Using Response.Filter
Jan 19, 2004 01:54 PM|iwilld0it|LINK
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
None
0 Points
7 Posts
Re: Using Response.Filter
Jan 19, 2004 10:24 PM|iwilld0it|LINK
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
None
0 Points
1 Post
Re: Using Response.Filter
Mar 18, 2012 11:01 PM|crowdy|LINK
mFilter.Write(mEncoding.GetBytes(output.ToString), 0, output.Length)
should be
None
0 Points
1 Post
Re: Using Response.Filter
Mar 23, 2012 07:56 AM|SujeetSrivastava|LINK
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.