The BeginRequest event is too early to enter the page. At this point in the request cycle, IIS / ASP.NET did not even decide to match your request with anything. Therefore, you should probably try something like the PostMapRequestHandler event.
However, this is not all he needs: at this moment the page (if there is one) has not yet been completed. This happens right between the PreRequestHandlerExecute and PostRequestHandlerExecute events . So, Pre ... too early, and Post ... too late. It is best to catch a page event, such as PreRenderComplete , and complete your injection.
public void Init(HttpApplication context) { context.PostMapRequestHandler += OnPostMapRequestHandler; } void OnPostMapRequestHandler(object sender, EventArgs e) { HttpContext context = ((HttpApplication)sender).Context; Page page = HttpContext.Current.CurrentHandler as Page; if (page != null) { page.PreRenderComplete += OnPreRenderComplete; } } void OnPreRenderComplete(object sender, EventArgs e) { Page page = (Page) sender;
ATTENTION: Few people still use them, but Server.Execute and Server.Transfer do not execute any pipeline events. Therefore, such child requests can never be caught using the IHttpModule .
Ruben
source share