I often use a feature, but I have not found any related solutions on the Internet. Today I have written two methods for converting absolute paths to virtual paths through project application.
Converts an absolute path under a Web site to a virtual path relative to a 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, type such as: http://www.cnblogs.com/</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 extracted from the first part of the first method.
Converts an absolute path under a Web site to a 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 = "page"> current page pointer, which is generally this </param>
/// <Param name = "specifiedpath"> absolute path </param>
/// <Returns> virtual path, for example :~ /</Returns>
Public static string convertspecifiedpathtorelativepath (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 dedicated to doing this.
Source: http://www.west263.com/www/info/60755-1.htm