HttpModule to Write Out JavaScript Script References to the Response
- by Chris
On my page in the Page_Load event I add a collection of strings to the Context object. I have an HttpModule that will fire EndRequest and retrieve the collection of strings. What I then do is write out a script reference tag (based on the collection of strings) to the response. The problem is that the page reads the script reference but doesn't retrieve the contents of the file (I imagine because this is occurring in the EndRequest event). I can't fire the BeginRequest event because I won't have access to the Context Items collection.
I tried to also registering an HttpHandler which Processes the request of the script reference but I can't access the collection of strings in the Context.Items from there. Any suggestions?
Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
Context.Items.Add("ScriptFile", "/UserControls.js");
}
HttpModule:
public void OnEndRequest(Object s, EventArgs e)
{
HttpApplication app = s as HttpApplication;
object script = app.Context.Items["ScriptFile"];
app.Response.Write("<script type='text/javascript' src='" + script + "'></script>");
}