Using HTTP Module to Change Response

L

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("<TITLE>TitleOfPageToBeModified</
TITLE>") Then
OriginalResponse.Replace("StringToRemoveFromBody", "")
'''Add "StringToPlaceInHeader" into <head> 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?
 
G

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top