ASP.NET: entering content into all response streams

We have the hudrends of an .aspx and html file on our website, and we would like to add a little javascript at the end of each file. If I wanted to do this using the HttpModule (trying to learn something new), it would 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();
}
+5
source share
1 answer

You probably know that an easy way to do this is to use static user controls in your website’s homepage file, like the old include SSI files?

About the HttpModule, you should be aware of content that is not HTML, but it looks like you have already deleted it with isAspxOrHtmlFile(). Seems good to me.

+4
source

All Articles