ASP. NET root path acquisition
Private string _ applicationpath;
/// <Summary>
/// Virtual Application Program Root path
/// </Summary>
Public String applicationpath
{
Get
{
_ Applicationpath = httpcontext. Current. Request. applicationpath;
If (_ applicationpath. Length = 1)
{
_ Applicationpath = "";
}
Return _ applicationpath;
}
}
Private string _ currentpath;
/// <Summary>
/// The current absolute path
/// </Summary>
Public String currentpath
{
Get
{
_ Currentpath = httpcontext. Current. server. mappath ("."). tolower (); // The current absolute path ("." In mappath indicates the current server)
If (_ currentpath. Length = 1)
{
_ Currentpath = "";
}
Return _ currentpath;
}
}
Private string _ rootpath;
/// <Summary>
/// System root directory
/// </Summary>
Public String rootpath
{
Get
{
_ Rootpath = httpcontext. Current. server. mappath (httpcontext. Current. Request. applicationpath). tolower (); // The current absolute path
If (_ rootpath. Length = 1)
{
_ Rootpath = "";
}
Return _ rootpath;
}
}
Set Web The absolute path under the site is converted to the virtual path relative to the specified page
First Method
/** // <Summary>
///
Set Web The absolute path under the site is converted to the virtual path relative to the specified page
/// </Summary>
/// <Param name = "page"> The current page pointer, usually This </param>
/// <Param name = "specifiedpath"> Absolute path </Param>
/// <Returns> Virtual Path , Type : Http://www.cnblogs.com/</returns>
Public static string convertspecifiedpathtorelativepathforpage (page, string specifiedpath)
{
// Root directory Virtual Path
String virtualpath = page. Request. applicationpath;
// Absolute root directory path
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 }\" Virtual Path instead of absolute path ! ", Specifiedpath ));
} // Convert to relative path
//( Test found that, Pathrooted In Vs2005 The built-in server follows IIS The running of the root directory or virtual directory is different. ,
// This part will be added later."\",Some do not., Determine 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 from the previous one "" Value
Int pagenodescount = pagenodes. Length-2; For (INT I = 0; I <pagenodescount; I)
{
Relativepath = "/.." relativepath;
} If (pagenodescount> 0)
{
// If yes "..", Then "/" Remove
Relativepath = relativepath. substring (1, relativepath. Length-1 );
} Return relativepath;
} Method 2 Set Web Convert the absolute path under the site to a virtual path
/** // <Summary>
/// Set Web Convert the absolute path under the site to a virtual path
/// Note: Non- Web Do not switch under the site
/// </Summary>
/// <Param name = "page"> The current page pointer, usually This </param>
/// <Param name = "specifiedpath"> Absolute path </Param>
/// <Returns> Virtual Path , Type :~ /</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.,Httprequest. mappathThe method is dedicated to doing this.
From http://www.notsee.info/