日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WebHelper类

發布時間:2023/12/1 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WebHelper类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

成員一:獲取上下文對象:

??????? public static HttpContext GetContext()
??????? {
??????????? HttpContext context = HttpContext.Current;
??????????? if (context == null)
??????????? {
??????????????? throw new Exception("HttpContext not found");??
??????????? }
??????????? return context;
??????? }

成員二:獲取當前程序集的版本號:

?????? public static string GetVersion()
??????? {
??????????? if (string.IsNullOrEmpty(WebHelper._version))
??????????? {
??????????????? int majorVersion = typeof(WebHelper).Assembly.GetName().Version.Major;
??????????????? WebHelper._version = majorVersion.ToString();
??????????? }
??????????? return WebHelper._version;
??????? }

成員三:獲取當前程序集中的資源字節數組

??????? public static byte[] LoadAssemblyFiles(string fullResourceName)
??????? {
??????????? if (string.IsNullOrEmpty(fullResourceName))
??????????????? throw new ArgumentNullException("fullResourceName");
??????????? byte[] fileContent;
??????????? using (Stream stream = typeof(WebHelper).Assembly.GetManifestResourceStream(fullResourceName))
??????????? {
??????????????? fileContent = new byte[stream.Length];
??????????????? stream.Read(fileContent, 0, fileContent.Length);
??????????? }
??????????? return fileContent;
??????? }

注意:紅色字體的WebHelper是這個函數成員所在的類,具體應用時,應該替換為實際的類名。例如如果這個成員是位于WebUtility類中,你必須將紅色字體處改成WebUtility。

成員四:從Cache中讀取緩存的資源字節數組。

??????? private static byte[] LoadFileFromCache(string fullResourceName)
??????? {
??????????? byte[] buffer;
??????????? HttpContext context = GetContext();
??????????? if (context.Cache[fullResourceName] == null)
??????????? {
??????????????? context.Cache[fullResourceName] = LoadAssemblyFiles(fileName);
??????????? }
??????????? buffer = (byte[])context.Cache[fullResourceName];
??????????? return buffer;
??????? }

成員五:從程序集中讀取資源文件(字符串)

??????? public static StringBuilder GetHtml(string fullResourceName)
??????? {
??????????? byte[] buffer = LoadFileFromCache(fullResourceName);
??????????? if (buffer == null || buffer.Length == 0)
??????????? {
??????????????? throw new ArgumentNullException("fullResourceName", ("isn't find " + fullResourceName));
??????????? }
??????????? return new StringBuilder(Encoding.Default.GetString(buffer));
??????? }

成員六:判斷IE瀏覽器的版本。

??????? public static bool isAccordantBrowser()
??????? {
??????????? HttpBrowserCapabilities bc = GetContext().Request.Browser;
??????????? if (bc.Browser != "IE" || float.Parse(bc.Version) < 5.5)
??????????? {
??????????????? return false;
??????????? }
??????????? return true;
??????? }

成員七:轉換文件長度為字符串

??????? public static string GetFormatString(double size)
??????? {
??????????? string sizeString = string.Empty;
??????????? if (size >= 1048576)
??????????? {
??????????????? sizeString = (Math.Round(size / 1048576, 2) + "MB");
??????????? }
??????????? else if (size > 1024)
??????????? {
??????????????? sizeString = (Math.Round(size / 1024, 2) + "KB");
??????????? }
??????????? else
??????????? {
??????????????? sizeString = (size + "B");
??????????? }
??????????? return sizeString;
??????? }

成員八:轉換時間變量字符串

??????? public static string GetFormatString(TimeSpan span)
??????? {
??????????? string timeString = string.Empty;
??????????? if (span.Days > 0 || span.Hours > 0)
??????????? {
??????????????? int hours = ((0x18 * span.Days) + span.Hours);
??????????????? timeString = (timeString + hours + "&nbsp;Hour(s)&nbsp;");
??????????? }
??????????? if (span.Minutes > 0)
??????????? {
??????????????? timeString = (timeString + span.Minutes + "&nbsp;Minute(s)&nbsp;");
??????????? }
??????????? if (span.Seconds > 0)
??????????? {
??????????????? timeString = (timeString + span.Seconds + "&nbsp;Second(s)&nbsp;");
??????????? }
??????????? return timeString;
??????? }

轉載于:https://www.cnblogs.com/jameszou/articles/1322327.html

總結

以上是生活随笔為你收集整理的WebHelper类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。