开发者

ASP.NET: Injecting content into all Response streams

We've got hudrend开发者_JS百科s 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();
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜