A function that is often used, but I have never found a relevant solution on the Internet. Today, I took the opportunity of project application to write two methods of converting absolute paths into virtual paths and encapsulating them. The path is converted to a virtual path relative to the specified page
/**//// <summary>
/// Convert the absolute path under the website to a virtual path relative to the specified page
/// </summary>
/// <param name="page">Current page pointer, usually this</param>
/// <param name="specifiedPath">Absolute path</param>
/// <returns>Virtual path, type: ../../</returns>
public static string ConvertSpecifiedPathToRelativePathForPage(Page page, string specifiedPath)
{
//Root directory virtual path
string virtualPath = page.Request.ApplicationPath;
//Absolute path to root directory
string pathRooted = HostingEnvironment.MapPath(virtualPath);
//Page virtual path
string pageVirtualPath = page.Request.Path;
if (!Path.IsPathRooted(specifiedPath) || specifiedPath.IndexOf(pathRooted) == -1)
{
throw new Exception(string.Format(""{0}"is a virtual path not an absolute path!", specifiedPath));
}
// Convert to relative path
//(The test found that pathRooted seems to be different when running on the server that comes with VS2005 and in the root directory or virtual directory under IIS.
// Some places will add "" after it, some won't, just make a judgment to be on the safe side)
if (pathRooted.Substring(pathRooted.Length - 1, 1) == "\")
{
specifiedPath = specifiedPath.Replace(pathRooted, "/");
}
else
{
specifiedPath = specifiedPath.Replace(pathRooted, "");
}
string relativePath = specifiedPath.Replace("\", "/");
string[] pageNodes = pageVirtualPath.Split('/');
// Subtract the last page and the previous "" value
int pageNodesCount = pageNodes.Length - 2;
for (int i = 0; i < pageNodesCount; i++)
{
relativePath = "/.." + relativePath;
}
if (pageNodesCount > 0)
{
// If ".." exists, remove the leading "/"
relativePath = relativePath.Substring(1, relativePath.Length - 1);
}
return relativePath;
}
The second method is obviously extracted from the first part of the first method, so I am too lazy to add relevant comments :P
Convert the absolute path under the website to a virtual path
/**//// <summary>
/// Convert the absolute path under the website to a virtual path
/// Note: Those under non-Web sites will not be converted.
/// </summary>
/// <param name="page">Current page pointer, usually this</param>
/// <param name="specifiedPath">Absolute path</param>
/// <returns>Virtual path, type: ~/</returns>
public static string ConvertSpecifiedPathToRelativePath(Page page, string specifiedPath)
{
string virtualPath = page.Request.ApplicationPath;
string pathRooted = HostingEnvironment.MapPath(virtualPath);
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;
}
There is nothing to say about converting a virtual path to an absolute path. The HttpRequest.MapPath method is specifically designed to do this.