ControlHelper.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web;
  6. namespace Bowin.Web.Controls.Mvc
  7. {
  8. public static class ControlHelper
  9. {
  10. static System.Collections.Concurrent.ConcurrentDictionary<HttpContext, IList<string>> scriptRegisterCache;
  11. static ControlHelper()
  12. {
  13. scriptRegisterCache = new System.Collections.Concurrent.ConcurrentDictionary<HttpContext, IList<string>>();
  14. scriptRegisterCache.TryAdd(HttpContext.Current, new List<string>());
  15. }
  16. public static bool IsRegisterScript(this HttpContext httpContext, string scriptKey)
  17. {
  18. return GetRegisterKeyList(httpContext).Contains(scriptKey);
  19. }
  20. public static void RegisterScript(this HttpContext httpContext, string scriptKey)
  21. {
  22. if (IsRegisterScript(httpContext, scriptKey))
  23. {
  24. throw new Exception(string.Format("当前HttpContext已经脚本key:{0}", scriptKey));
  25. }
  26. GetRegisterKeyList(httpContext).Add(scriptKey);
  27. }
  28. public static IList<string> GetRegisterKeyList(this HttpContext httpContext)
  29. {
  30. IList<string> scriptKeyList = new List<string>();
  31. var isRegisterContext = scriptRegisterCache.TryGetValue(httpContext, out scriptKeyList);
  32. if (!isRegisterContext)
  33. {
  34. scriptRegisterCache.TryAdd(HttpContext.Current, new List<string>());
  35. scriptKeyList = new List<string>();
  36. }
  37. return scriptKeyList;
  38. }
  39. }
  40. }