HttpModule to Write Out JavaScript Script References to the Response

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-03-11T16:28:47Z Indexed on 2010/03/11 21:04 UTC
Read the original article Hit count: 402

Filed under:
|
|
|
|

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>");
    }

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about c#