Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / April 2008

Tip: Looking for answers? Try searching our database.

Using HTTP Module to Change Response

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lundk01@hotmail.com - 22 Apr 2008 19:56 GMT
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?
George Ter-Saakov - 22 Apr 2008 20:50 GMT
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.

> Is it possible to use an HTTP Module to change the source code of a
> page?
[quoted text clipped - 53 lines]
>
> Any ideas?

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.