ASP. NET root path acquisition and conversion of absolute paths under the web site to virtual paths

Source: Internet
Author: User

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/

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.