实现IHttpModule接口,给每个页面输出一段脚本

在App_Code文件中添加TGModule.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
///TGModule 的摘要说明
/// </summary>
public class TGModule : IHttpModule
{
    public void Dispose() { }
    public void Init(HttpApplication context)
    {
        context.EndRequest += new EventHandler(context_EndRequest);
    }
    void context_EndRequest(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        HttpContext context = application.Context;
        HttpRequest request = application.Request;
        HttpResponse response = application.Response;

        string uri = request.RawUrl;

        string a = uri.Substring(uri.LastIndexOf(".") + 1);

        //只拦截aspx html页面
        if (a.Contains("aspx") || a.Contains("html"))
        {
            context.Response.Write(@"<script type='text/javascript' src='/zhuanti/tuangou.js'></script>");
        }
    }
}

在web.config中配置

<httpModules>
      <add name="TGModule" type="TGModule"/>
   
</httpModules>

完成!这样在每个页面的末尾就自动添加了一段脚本 而不会影响到css js文件

<script type='text/javascript' src='/zhuanti/tuangou.js'></script>

 

如图:虽然在html标签外部 但是还是可以运行的。

 

关于IhttpModule详细介绍可以看这里:http://www.cnblogs.com/chenlulouis/archive/2009/12/18/1626918.html

posted @ 2015-01-02 17:18  思如雨  阅读(311)  评论(0编辑  收藏  举报