HtmlHelperExtensions.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web;
  5. using Microsoft.AspNetCore.Html;
  6. using Microsoft.AspNetCore.Mvc.Rendering;
  7. using WMS.Util;
  8. using WMS.Core;
  9. using WMS.BZWeb.Extensions;
  10. using Microsoft.AspNetCore.Mvc.ViewFeatures;
  11. namespace WMS.BZWeb
  12. {
  13. /// <summary>
  14. /// 描 述:对HtmlHelper类进行扩展
  15. /// </summary>
  16. public static class HtmlHelperExtensions
  17. {
  18. private const ERedisCacheNo RedisCacheNo = ERedisCacheNo.CssJs;
  19. /// <summary>
  20. /// 往页面中写入js文件
  21. /// </summary>
  22. /// <param name="htmlHelper">需要扩展对象</param>
  23. /// <param name="jsFiles">文件路径</param>
  24. /// <returns></returns>
  25. public static HtmlString AppendJsFile(this IHtmlHelper htmlHelper, params string[] jsFiles)
  26. {
  27. //string jsfilename = "script_" + string.Join("_", jsFiles);
  28. //string jsStr = RedisCache.Read<string>(jsfilename, RedisCacheNo);
  29. //if (string.IsNullOrWhiteSpace(jsStr))
  30. //{
  31. // jsStr = JsCssUtil.ReadJSFile(jsFiles, SysSetCore.GetSysSet().IsJsCssCompressor);
  32. // jsStr = string.Format("<script>{0}</script>", jsStr);
  33. // RedisCache.Write<string>(jsfilename, jsStr, RedisCacheNo);
  34. //}
  35. string jsStr = JsCssUtil.ReadJSFile(jsFiles, SysSetCore.GetSysSet().IsJsCssCompressor);
  36. jsStr = string.Format("<script>{0}</script>", jsStr);
  37. return new HtmlString(jsStr);
  38. }
  39. /// <summary>
  40. /// 往页面中写入js文件
  41. /// </summary>
  42. /// <param name="htmlHelper">需要扩展对象</param>
  43. /// <param name="jsFiles">文件路径</param>
  44. /// <returns></returns>
  45. //public static HtmlString AppendJsFile(this IHtmlHelper htmlHelper, params string[] jsFiles)
  46. //{
  47. // if (htmlHelper is null)
  48. // {
  49. // throw new System.ArgumentNullException(nameof(htmlHelper));
  50. // }
  51. // string jsFile = "";
  52. // foreach (string file in jsFiles)
  53. // {
  54. // if (jsFile != "")
  55. // {
  56. // jsFile += ",";
  57. // }
  58. // jsFile += file;
  59. // }
  60. // string jsStr = "";
  61. // if (ConfigHelper.GetValue<string>("env") != "dev")
  62. // {
  63. // jsStr = ConfigHelper.GetCache(jsFile);
  64. // }
  65. // if (string.IsNullOrEmpty(jsStr))
  66. // {
  67. // jsStr = JsCssHelper.ReadJSFile(jsFiles);
  68. // ConfigHelper.SetCache(jsFile, jsStr);
  69. // }
  70. // StringBuilder content = new StringBuilder();
  71. // string jsFormat = "<script>{0}</script>";
  72. // content.AppendFormat(jsFormat, jsStr);
  73. // return new HtmlString(content.ToString());
  74. //}
  75. public static HtmlString AppendString(this IHtmlHelper htmlHelper, string jsFiles)
  76. {
  77. return new HtmlString(jsFiles);
  78. }
  79. /// <summary>
  80. /// 往页面中写入js文件
  81. /// </summary>
  82. /// <param name="htmlHelper">需要扩展对象</param>
  83. /// <param name="jsFiles">文件路径</param>
  84. /// <returns></returns>
  85. public static HtmlString AppendJsFileByLocation(this IHtmlHelper htmlHelper, params string[] jsFiles)
  86. {
  87. //string jsfilename = "script_" + string.Join("_", jsFiles);
  88. //string jsStr = RedisCache.Read<string>(jsfilename, RedisCacheNo);
  89. //if (string.IsNullOrWhiteSpace(jsStr))
  90. //{
  91. // jsStr = JsCssUtil.ReadJSFile(jsFiles, false);
  92. // jsStr = string.Format("<script>{0}</script>", jsStr);
  93. // RedisCache.Write<string>(jsfilename, jsStr, RedisCacheNo);
  94. //}
  95. string jsStr = JsCssUtil.ReadJSFile(jsFiles, false);
  96. jsStr = string.Format("<script>{0}</script>", jsStr);
  97. return new HtmlString(jsStr);
  98. }
  99. /// <summary>
  100. /// 往页面中写入css样式
  101. /// </summary>
  102. /// <param name="htmlHelper">需要扩展对象</param>
  103. /// <param name="cssFiles">文件路径</param>
  104. /// <returns></returns>
  105. //public static HtmlString AppendCssFile(this IHtmlHelper htmlHelper, params string[] cssFiles)
  106. //{
  107. // if (htmlHelper is null)
  108. // {
  109. // throw new System.ArgumentNullException(nameof(htmlHelper));
  110. // }
  111. // string cssFile = "";
  112. // foreach (string file in cssFiles)
  113. // {
  114. // if (cssFile != "")
  115. // {
  116. // cssFile += ",";
  117. // }
  118. // cssFile += file;
  119. // }
  120. // string cssStr = "";
  121. // if (ConfigHelper.GetValue<string>("env") != "dev")
  122. // {
  123. // cssStr = ConfigHelper.GetCache(cssFile);
  124. // }
  125. // if (string.IsNullOrEmpty(cssStr))
  126. // {
  127. // var url = ConfigHelper.GetConfig().VirtualPath;
  128. // cssStr = JsCssHelper.ReadCssFile(cssFiles);
  129. // if (url != "")
  130. // {
  131. // cssStr = cssStr.Replace("url(", "url(" + url);
  132. // }
  133. // ConfigHelper.SetCache(cssFile, cssStr);
  134. // }
  135. // StringBuilder content = new StringBuilder();
  136. // string cssFormat = "<style>{0}</style>";
  137. // content.AppendFormat(cssFormat, cssStr);
  138. // return new HtmlString(content.ToString());
  139. //}
  140. /// <summary>
  141. /// 往页面中写入css样式
  142. /// </summary>
  143. /// <param name="htmlHelper">需要扩展对象</param>
  144. /// <param name="cssFiles">文件路径</param>
  145. /// <returns></returns>
  146. public static HtmlString AppendCssFile(this IHtmlHelper htmlHelper, params string[] cssFiles)
  147. {
  148. //string cssfilename = "style_" + string.Join("_", cssFiles);
  149. //string cssStr = RedisCache.Read<string>(cssfilename, RedisCacheNo);
  150. //if (string.IsNullOrWhiteSpace(cssStr))
  151. //{
  152. // cssStr = JsCssUtil.ReadCssFile(cssFiles, SysSetCore.GetSysSet().IsJsCssCompressor);
  153. // var url = HttpContext.Current.Request.ApplicationPath;
  154. // if (url != "/")
  155. // {
  156. // cssStr = cssStr.Replace("url(", "url(" + url);
  157. // }
  158. // cssStr = string.Format("<style>{0}</style>", cssStr);
  159. // RedisCache.Write<string>(cssfilename, cssStr, RedisCacheNo);
  160. //}
  161. string cssStr = JsCssUtil.ReadCssFile(cssFiles, SysSetCore.GetSysSet().IsJsCssCompressor);
  162. var url = ConfigHelper.GetConfig().VirtualPath;
  163. if (url != "/")
  164. {
  165. cssStr = cssStr.Replace("url(", "url(" + url);
  166. }
  167. cssStr = string.Format("<style>{0}</style>", cssStr);
  168. return new HtmlString(cssStr);
  169. }
  170. /// <summary>
  171. /// 获取虚拟目录
  172. /// </summary>
  173. /// <returns></returns>
  174. public static string GetVirtualPath(this IHtmlHelper htmlHelper)
  175. {
  176. if (htmlHelper is null)
  177. {
  178. throw new System.ArgumentNullException(nameof(htmlHelper));
  179. }
  180. return ConfigHelper.GetConfig().VirtualPath;
  181. }
  182. /// <summary>
  183. /// 设置当前页面地址
  184. /// </summary>
  185. /// <param name="htmlHelper"></param>
  186. /// <returns></returns>
  187. public static HtmlString SetCurrentUrl(this IHtmlHelper htmlHelper)
  188. {
  189. string currentUrl = "";//(string)WebHelper.GetHttpItems("currentUrl");
  190. return new HtmlString("<script>var lrCurrentUrl='" + currentUrl + "';var lrModuleButtonList;var lrModuleColumnList;var lrModule;</script>");
  191. }
  192. public static string GetUITheme()
  193. {
  194. return "lr-uitheme-top";
  195. }
  196. }
  197. }