WebSitePathHelper.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System.Web;
  2. namespace Ant.Service.Utilities
  3. {
  4. /// <summary>
  5. /// 网站路径操作类
  6. /// </summary>
  7. public static class WebSitePathHelper
  8. {
  9. /// <summary>
  10. ///
  11. /// </summary>
  12. public enum SortType
  13. {
  14. /// <summary>
  15. ///
  16. /// </summary>
  17. Photo = 1,
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. Article = 5,
  22. /// <summary>
  23. ///
  24. /// </summary>
  25. Diary = 7,
  26. /// <summary>
  27. ///
  28. /// </summary>
  29. Pic = 2,
  30. /// <summary>
  31. ///
  32. /// </summary>
  33. Music = 6,
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. AddressList = 4,
  38. /// <summary>
  39. ///
  40. /// </summary>
  41. Favorite = 3,
  42. }
  43. #region 根据给出的相对地址获取网站绝对地址
  44. /// <summary>
  45. /// 根据给出的相对地址获取网站绝对地址
  46. /// </summary>
  47. /// <param name="localPath">相对地址</param>
  48. /// <returns>绝对地址</returns>
  49. public static string GetWebPath(string localPath)
  50. {
  51. string path = HttpContext.Current.Request.ApplicationPath;
  52. string thisPath;
  53. string thisLocalPath;
  54. //如果不是根目录就加上"/" 根目录自己会加"/"
  55. if (path != "/")
  56. {
  57. thisPath = path + "/";
  58. }
  59. else
  60. {
  61. thisPath = path;
  62. }
  63. if (localPath.StartsWith("~/"))
  64. {
  65. thisLocalPath = localPath.Substring(2);
  66. }
  67. else
  68. {
  69. return localPath;
  70. }
  71. return thisPath + thisLocalPath;
  72. }
  73. #endregion
  74. #region 获取网站绝对地址
  75. /// <summary>
  76. /// 获取网站绝对地址
  77. /// </summary>
  78. /// <returns></returns>
  79. public static string GetWebPath()
  80. {
  81. string path = System.Web.HttpContext.Current.Request.ApplicationPath;
  82. string thisPath;
  83. //如果不是根目录就加上"/" 根目录自己会加"/"
  84. if (path != "/")
  85. {
  86. thisPath = path + "/";
  87. }
  88. else
  89. {
  90. thisPath = path;
  91. }
  92. return thisPath;
  93. }
  94. #endregion
  95. #region 根据相对路径或绝对路径获取绝对路径
  96. /// <summary>
  97. /// 根据相对路径或绝对路径获取绝对路径
  98. /// </summary>
  99. /// <param name="localPath">相对路径或绝对路径</param>
  100. /// <returns>绝对路径</returns>
  101. public static string GetFilePath(string localPath)
  102. {
  103. if (System.Text.RegularExpressions.Regex.IsMatch(localPath, @"([A-Za-z]):\\([\S]*)"))
  104. {
  105. return localPath;
  106. }
  107. else
  108. {
  109. return System.Web.HttpContext.Current.Server.MapPath(localPath);
  110. }
  111. }
  112. #endregion
  113. }
  114. }