Using HTTP Module to Change Response
am 22.04.2008 20:56:32 von lundk01
Is it possible to use an HTTP Module to change the source code of a
page?
In the source code for a particular ASPX file there are many
references to the same .Css file which I have been asked to remove
from the body and place once in the header of the file in order to
improve the performance.
Here is an example of the code for what I'd like to do:
Imports System.Web
Public Class MyModule
Implements IHttpModule
Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
End Sub
Public Sub Init(ByVal application As HttpApplication) Implements
IHttpModule.Init
AddHandler application.EndRequest, AddressOf
Me.Application_EndRequest
End Sub
Private Sub Application_EndRequest(ByVal source As Object, ByVal e
As EventArgs)
Dim application As HttpApplication = DirectCast(source,
HttpApplication)
Dim context As HttpContext = application.Context
Dim OriginalResponse As String = application.?????? 'Need to
extract HTML code behind page somehow!!!
If OriginalResponse.Contains("
TitleOfPageToBeModified
TITLE>") Then
OriginalResponse.Replace("StringToRemoveFromBody", "")
'''Add "StringToPlaceInHeader" into somehow!!!
context.Response.Write(OriginalResponse.ToString)
End If
End Sub
End Class
Can this be done this way? I would have thought that during the
EndRequest event I would have had access to the HTML, but I am unable
to get it. If I re-execute the page myself, I can get it using:
Dim str As System.IO.StringWriter = New System.IO.StringWriter
application.Server.Execute(application.Request.Path, str)
but I can't see how this will help. The application which produces
these cannot currently be modified so we are looking for a temporary
fix. I know there are other solutions (IISPROXY) but I have been asked
to look just at HttpModules.
Any ideas?
Re: Using HTTP Module to Change Response
am 22.04.2008 21:50:13 von George Ter-Saakov
I would add custom filter to the Response.object in Application_BeginRequest
MyFilter rp = new MyFilter(Response.Filter);
Response.Filter = rp;
Thus you are going to have a control on outputted HTML. So you filter will
analys the stream and modify it and send it to the original Response.Filter.
George.
wrote in message
news:f7f4cadb-13c7-48ab-827a-a952bf516ff0@s50g2000hsb.google groups.com...
> Is it possible to use an HTTP Module to change the source code of a
> page?
>
> In the source code for a particular ASPX file there are many
> references to the same .Css file which I have been asked to remove
> from the body and place once in the header of the file in order to
> improve the performance.
>
> Here is an example of the code for what I'd like to do:
>
> Imports System.Web
>
> Public Class MyModule
> Implements IHttpModule
>
> Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
> End Sub
>
> Public Sub Init(ByVal application As HttpApplication) Implements
> IHttpModule.Init
> AddHandler application.EndRequest, AddressOf
> Me.Application_EndRequest
> End Sub
>
> Private Sub Application_EndRequest(ByVal source As Object, ByVal e
> As EventArgs)
>
> Dim application As HttpApplication = DirectCast(source,
> HttpApplication)
> Dim context As HttpContext = application.Context
>
> Dim OriginalResponse As String = application.?????? 'Need to
> extract HTML code behind page somehow!!!
>
> If OriginalResponse.Contains("TitleOfPageToBeModified
> TITLE>") Then
> OriginalResponse.Replace("StringToRemoveFromBody", "")
> '''Add "StringToPlaceInHeader" into somehow!!!
> context.Response.Write(OriginalResponse.ToString)
> End If
>
> End Sub
> End Class
>
>
> Can this be done this way? I would have thought that during the
> EndRequest event I would have had access to the HTML, but I am unable
> to get it. If I re-execute the page myself, I can get it using:
>
> Dim str As System.IO.StringWriter = New System.IO.StringWriter
> application.Server.Execute(application.Request.Path, str)
>
> but I can't see how this will help. The application which produces
> these cannot currently be modified so we are looking for a temporary
> fix. I know there are other solutions (IISPROXY) but I have been asked
> to look just at HttpModules.
>
> Any ideas?