views:

67

answers:

1

We've got hudrends of ASPX and HTML file in our site, and would like to inject a small JavaScript at the end of each file. If I wanted to do this using and HttpModule (trying to learn something new), would this do--are there any glaring problems?

public void Init(HttpApplication context)
{
    context.BeginRequest += new EventHandler(context_BeginRequest);
}

void context_BeginRequest(object sender, EventArgs e)
{
    var context = HttpContext.Current;

    if (!isAspxOrHtmlFile(context.Request.Path)
    {
        return;
    }

    var javascript = ...

    using (var writer = new StringWriter())
    {
        context.Server.Execute(context.Request.Path, writer);
        context.Response.ContentType = "text/html";
        context.Response.Write(writer.GetStringBuilder().ToString() + javascript);
    }

    context.Response.End();
}
+2  A: 

You probably know easy way to do that is to using static custom user controls at your website master page file, just like old SSI include files, right?

About HttpModule, you should be aware for content which isn't HTML, but seems you already remove this by using isAspxOrHtmlFile(). Seems fine to me.

Rubens Farias
Yeah, I'm sure there's a better way to do this, I'm just using this as an example to get familiar with HttpModules. Thanks :)
JamesBrownIsDead