Search code examples
c#httpmodule.net-1.1

HttpModule - get HTML content or controls for modifications


Tried something like this:

HttpApplication app = s as HttpApplication; //s is sender of the OnBeginRequest event
System.Web.UI.Page p = (System.Web.UI.Page)app.Context.Handler;
System.Web.UI.WebControls.Label lbl = new System.Web.UI.WebControls.Label();
lbl.Text = "TEST TEST TEST";
p.Controls.Add(lbl);    

when running this I get "Object reference not set to an instance of an object." for the last line...

How do I get to insert two lines of text (asp.net/html) at specific loactions in the original file? And how do I figure out the extension of the file (I only want to apply this on aspx files...?


Solution

  • I'm not sure, but I don't think you can use an HttpModule to alter the Page's control tree (please correct me if I'm wrong). You CAN modify the HTML markup however, you'll have to write a "response filter" for this. For an example, see http://aspnetresources.com/articles/HttpFilters.aspx, or google for "httpmodule response filter".