Asp.net 学习资料

伦惠峰

将Web站点下的绝对路径转换为虚拟路径

    /// <summary>
    /// 将Web站点下的绝对路径转换为虚拟路径
    /// 注:非Web站点下的则不转换
    /// </summary>
    /// <param name="specifiedPath">绝对路径</param>
    /// <returns>虚拟路径, 型如: ~/</returns>
    public  string ConvertSpecifiedPathToRelativePath(string specifiedPath)
    {
        string pathRooted = Server.MapPath("~/");

        if (!Path.IsPathRooted(specifiedPath) || specifiedPath.IndexOf(pathRooted) == -1)
        {
            return specifiedPath;
        }

        if (pathRooted.Substring(pathRooted.Length - 1, 1) == "\\")
        {
            specifiedPath = specifiedPath.Replace(pathRooted, "~/");
        }
        else
        {
            specifiedPath = specifiedPath.Replace(pathRooted, "~");
        }

        string relativePath = specifiedPath.Replace("\\", "/");
        return relativePath;
    }

posted on 2007-05-29 22:45  伦惠峰  阅读(231)  评论(0)    收藏  举报

导航