zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.UI.HtmlControls;
using System.IO;
using System.IO.Compression;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections.Generic;
 
namespace HH.WMS.Utils
{
    /// <summary>
    /// WEB请求上下文信息工具类
    /// </summary>
    public partial class ZHttp
    {
        #region 本地路径转换成URL相对路径
        //本地路径转换成URL相对路径
        public static string ServerPathToUrl(string ServerPath)
        {
            string tmpRootDir = HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
            string sUrl = ServerPath.Replace(tmpRootDir, ""); //转换成相对路径
            sUrl = "/" + sUrl.Replace(@"\", @"/");
            return sUrl;
        }
        #endregion
 
        #region 解析地址为客户端地址
        /// <summary>
        /// 把一个URL路径转换为绝对路径
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string ResolveUrl(string url)
        {
            //空地址, 绝对地址, 不以~开头的地址
            if (string.IsNullOrEmpty(url) || url.Contains("://") || !url.StartsWith("~"))
            {
                return url;
            }
 
            int queryindex = url.IndexOf('?');
            if (queryindex != -1)
            {
                string queryString = url.Substring(queryindex);
                string baseUrl = url.Substring(0, queryindex);
 
                return string.Concat(VirtualPathUtility.ToAbsolute(baseUrl), queryString);
            }
            else
            {
                return VirtualPathUtility.ToAbsolute(url);
            }
        }
        #endregion
 
        #region 获取WEB应用程序的根目录
        /// <summary>
        /// 获取WEB应用程序的根目录(包含虚拟目录路径),以/结尾 
        /// </summary>
        public static string RootPath
        {
            get
            {
                string url = HttpContext.Current.Request.ApplicationPath;
                if (url.EndsWith("/"))
                {
                    return url;
                }
                return url + "/";
            }
        }
 
        /// <summary>
        /// 获取WEB应用程序的根目录全路径(包含虚拟目录路径),以/结尾 
        /// </summary>
        public static string RootFullPath
        {
            get
            {
                if (Port == 80)
                {
                    return string.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, Host, RootPath);
                }
                else
                {
                    return string.Format("{0}://{1}:{2}{3}", HttpContext.Current.Request.Url.Scheme, Host, Port, RootPath);
                }
            }
        }
        #endregion
 
        #region 取得网站根目录的物理路径
        /// <summary>
        /// 取得网站根目录的物理路径
        /// </summary>
        public static string RootPhysicalPath
        {
            get
            {
                return System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
            }
        }
        #endregion
 
        #region 获取网站的端口
        /// <summary>
        /// 端口
        /// </summary>
        public static int Port
        {
            get
            {
                return HttpContext.Current.Request.Url.Port;
            }
        }
        #endregion
    }
}