将Web站点下的绝对路径转换为虚拟路径
/// <summary>
/// 将Web站点下的绝对路径转换为虚拟路径
/// 注:非Web站点下的则不转换
/// </summary>
/// <param name="specifiedPath">绝对路径</param>
/// <returns>虚拟路径, 型如: ~/</returns>
public string ConvertSpecifiedPathToRelativePath(string specifiedPath)
{
string pathRooted = Server.MapPath("~/");
/// 将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;
}
浙公网安备 33010602011771号