您的位置:首页 > 运维架构 > 网站架构

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

2017-03-15 09:15 337 查看
如标题给出示例,例如:给出 /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;

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: