专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

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

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:58:07       共计:3660 浏览

在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>




版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:ASP.NET之HttpModule拦截404异常 | ·下一条:iis html文件打开404,c# – 使用HttpModule处理html文件以捕获IIS7上的404错误

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有