Converts an absolute path under a Web site to a virtual path.

Source: Internet
Author: User
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.

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.