views:

22

answers:

1

Hi,

I want to change page content while it is going from the server because i need to add some advertisements inside the html elements that are advertisement holder.

protected void Application_PreSendRequestContent(object sender, EventArgs e) this is good but i couldn't get access to HttpContext . Should i, i don't know :)

But in this method: protected void Application_EndRequest(object sender, EventArgs e) i could get the HttpContext but i couldn't find the server response in it.

How can i do this?

A: 

You might want to implement a HttpModule instead of global.asax. You can find an example of a module that manipulates the response in MSDN: Walkthrough: Creating and Registering a Custom HTTP Module

See also this page for some additional information (e.g. why a HttpModule instead of global.asax): HTTP Handlers and HTTP Modules Overview


To answer your comment: here are some reasons why to use a module instead of global.asax (have a look at the document linked above for more information):

  • You can implement much of the functionality of a module in the application's Global.asax file [...] however, modules have an advantage over the Global.asax file because they are encapsulated and can be created one time and used in many different applications.
  • In IIS 7.0, the integrated pipeline enables managed modules to subscribe to pipeline notifications for all requests, not just requests for ASP.NET resources.
  • You can enable/disable a module via web.config (without touching any code)

You should use a module whenever you must create code that depends on application events, and when the following conditions are true:

  • You want to re-use the module in other applications.
  • You want to avoid putting complex code in the Global.asax file.
  • The module applies to all requests in the pipeline (IIS 7.0 Integrated mode only).
M4N
Why do you offer HttpModudle instead of Global.asax? Can't i access to content of page?
uzay95