Using HTTP Module to Change Response

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