根据给出的相对地址获取网站绝对地址

来源:互联网 发布:大数据分析平台架构 编辑:程序博客网 时间:2024/06/05 06:40

如标题给出示例,例如:给出 /login.aspx 可以返回http://www.onewill.com,cn/login.aspx 的绝对地址结果。


/// 根据给出的相对地址获取网站绝对地址
        /// </summary>
        /// <param name="localPath">相对地址</param>
        /// <returns>绝对地址</returns>
        public string GetWebPath(string localPath)
        {
            string path = HttpContext.Current.Request.ApplicationPath;
            string thisPath;
            string thisLocalPath;
            //如果不是根目录就加上"/" 根目录自己会加"/"
            if (path != "/")
            {
                thisPath = path + "/";
            }
            else
            {
                thisPath = path;
            }
            if (localPath.StartsWith("~/"))
            {
                thisLocalPath = localPath.Substring(2);
            }
            else
            {
                return localPath;
            }
            return thisPath + thisLocalPath;
        }

1 0
原创粉丝点击