Is there any way to get acces and change to Page before leave the server?
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, Eve开发者_C百科ntArgs e)
i could get the HttpContext
but i couldn't find the server response in it.
How can i do this?
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).
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 :)
You can always get access to the HttpContext for the current request by using HttpContext.Current
精彩评论