找回密码
 立即注册
首页 业界区 业界 一个ASP.NET的JS管理方案

一个ASP.NET的JS管理方案

凌彦慧 2025-5-29 20:37:12
场景:在自定义控件、用户控件、页面、后台代码都会有引用JS的可能,这就会出现混乱或者重复引用的可能。
一个自定义控件,用于在ASPX页面中注册JS:
  1. public class Script : Control
  2. {
  3.     #region 属性
  4.     private string m_Src;
  5.     /// <summary>
  6.     /// 脚本文件路径
  7.     /// </summary>
  8.     public string Src
  9.     {
  10.         get { return m_Src; }
  11.         set { m_Src = value; }
  12.     }
  13.     #endregion
  14.     /// <summary>
  15.     /// 在控件Init的时候将JS路径添加到HttpContext.Current.Items["IncludedJavaScript"]中。
  16.     /// </summary>
  17.     /// <param name="e"></param>
  18.     protected override void OnInit(EventArgs e)
  19.     {
  20.         base.OnInit(e);
  21.         if (!string.IsNullOrEmpty(Src))
  22.         {
  23.             string src = ResolveUrl(Src);
  24.             List<string> includedJs = HttpContext.Current.Items["IncludedJavaScript"] as List<string>;
  25.             if (null == includedJs)
  26.             {
  27.                 includedJs = new List<string>();
  28.                 HttpContext.Current.Items["IncludedJavaScript"] = includedJs;
  29.             }
  30.             
  31.             if (!includedJs.Contains(src))
  32.             {
  33.                 includedJs.Add(src);
  34.             }
  35.             
  36.         }
  37.     }
  38. }
复制代码
 
一个静态类,用于管理JS和在后台代码(cs文件)中注册JS:
  1. /// <summary>
  2. /// Javascript管理器
  3. /// </summary>
  4. public static class JavaScriptManager
  5. {
  6.     /// <summary>
  7.     /// 包含JS引用。
  8.     /// </summary>
  9.     /// <param name="filePaths"></param>
  10.     public static void Include(params string[] filePaths)
  11.     {
  12.         HttpContext context = HttpContext.Current;
  13.         if (null == context)
  14.         {
  15.             throw new Exception("HttpContext为空。");
  16.         }
  17.         System.Web.UI.Page p = context.CurrentHandler as System.Web.UI.Page;
  18.         if (null == p)
  19.         {
  20.             throw new Exception("HttpContext.CurrentHandler不是Page。");
  21.         }
  22.         IList<string> jss = GetIncludedJavaScript();
  23.         string resolveUrl;
  24.         foreach (string filePath in filePaths)
  25.         {
  26.             resolveUrl=p.ResolveUrl(filePath);
  27.             if (!jss.Contains(resolveUrl))
  28.             {
  29.                 jss.Add(p.ResolveUrl(resolveUrl));
  30.             }
  31.         }
  32.     }
  33.     /// <summary>
  34.     /// 获取已经包含的JS列表
  35.     /// </summary>
  36.     /// <returns></returns>
  37.     public static IList<string> GetIncludedJavaScript()
  38.     {
  39.         HttpContext context = HttpContext.Current;
  40.         if (null == context)
  41.         {
  42.             throw new Exception("HttpContext为空。");
  43.         }
  44.         IList<string> jss = HttpContext.Current.Items["IncludedJavaScript"] as IList<string>;
  45.         if (null == jss)
  46.         {
  47.             jss = new List<string>();
  48.             HttpContext.Current.Items["IncludedJavaScript"] = jss;
  49.         }
  50.         return jss;
  51.     }
  52. }
复制代码
 
然后写一个基类页面,所有的页面都要继承自这个基类页:
  1. public class BasePage : System.Web.UI.Page
  2. {
  3.     public BasePage() { }
  4.     #region 注册/管理JS引用
  5.     /// <summary>
  6.     /// 将引用的JS添加到Page.Head中。
  7.     /// </summary>
  8.     private void InitJS()
  9.     {
  10.         IList<string> includedJs = JavaScriptManager.JavaScriptManager.GetIncludedJavaScript();
  11.         foreach (string jsFilePath in includedJs)
  12.         {
  13.             var script = new HtmlGenericControl("script");
  14.             script.Attributes["type"] = "text/javascript";
  15.             script.Attributes["src"] = jsFilePath;
  16.             Page.Header.Controls.Add(script);
  17.         }
  18.     }
  19.     /// <summary>
  20.     /// 在呈现之前注册JS
  21.     /// </summary>
  22.     /// <param name="e"></param>
  23.     protected override void OnPreRender(EventArgs e)
  24.     {
  25.         base.OnPreRender(e);
  26.         InitJS();
  27.     }
  28.     #endregion
  29. }
复制代码
上面是在OnPreRender中将JS注册到Page.Head中的,所以如果在自定义控件中注册JS引用,请在OnPreRender之前引用。
在ASPX页面中注册JS:
  1. <html xmlns="http://www.w3.org/1999/xhtml" >
  2. <head runat="server">
  3.     <title></title>
  4.     <lulu:Script runat="server" ID="Script1" Src="~/Test/aspx.js"></lulu:Script>
  5. </head>
  6. <body>
  7.     <form id="form1" runat="server">
  8.    
  9.         <lulu:Script runat="server" ID="Script5" Src="~/Test/aspx.js"></lulu:Script>
  10.         <lulu:Script runat="server" ID="Script6" Src="~/Test/aspx2.js"></lulu:Script>
  11.         <lulu:Script runat="server" ID="Script7" Src="~/Test/aspx.js"></lulu:Script>
  12.    
  13.     </form>
  14. </body>
  15. </html>
复制代码
 
在CS页面中注册JS:
  1. public partial class _Default : BasePage
  2. {
  3.     protected void Page_Load(object sender, EventArgs e)
  4.     {
  5.         JavaScriptManager.JavaScriptManager.Include("~/JS/cs.js",
  6.            "~/JS/cs.js",
  7.            "~/JS/cs.js2",
  8.            "~/JS/cs.js");
  9.     }
  10. }
复制代码
最终生成的页面:
1.png

 
在这里是使用HttpContext.Current.Items来保存包含的JS列表。欢迎讨论、改进。
测试代码:JsManager.rar
作者:QLeelulu
2.png

出处:http://QLeelulu.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册