A feature that is often used, but has not been found on the internet for related solutions, today, with the opportunity of project application, I wrote two methods to convert the absolute path under the web site to the virtual path relative to the specified page.
/** // <Summary>
/// Convert the absolute path under the web site to the virtual path relative to the specified page
/// </Summary>
/// <Param name = "page"> current page pointer, which is generally this </param>
/// <Param name = "specifiedpath"> absolute path </param>
/// <Returns> virtual path, for example,.../</returns>
Public static string convertspecifiedpathtorelativepathforpage (page, string specifiedpath)
{
// Root directory Virtual Path
String virtualpath = page. Request. applicationpath;
// Absolute path of the 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 rather than an absolute path! ", Specifiedpath ));
}
// Convert to relative path
// (The test showed that the server that comes with pathrooted in vs2005 does not seem to run in the root directory or virtual directory of IIS,
// "\" Will be added at the end of this place, and some will not. Please check for the sake of insurance)
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 first "/"
Relativepath = relativepath. substring (1, relativepath. Length-1 );
}
Return relativepath;
}
The second method is obviously extracted from the first part of the first method, so you are too lazy to add comments to convert the absolute path under the web site to the virtual path.
/** // <Summary>
/// Convert the absolute path under the web site to a virtual path
/// Note: Non-web sites do not convert
/// </Summary>
/// <Param name = "specifiedpath"> absolute path </param>
/// <Returns> virtual path, for example :~ /</Returns>
Public static string convertspecifiedpathtorelativepath (string specifiedpath)
{
String pathrooted = hostingenvironment. 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;
}
There is nothing to say about converting a virtual path to an absolute path. The httprequest. mappath method is dedicated to doing this.