Reversing MapPath)

Source: Internet
Author: User

The first thing to note is that the results obtained by Google are incorrect because the virtual root directory of the application is not.

For example, if the physical root directory of the application is c: \ website \ piic \ en, the virtual path of the application is/en/, and the file path is c: for \ websites \ piic \ en \ images \ aaa.jpg, according to the solutions, the result will be/images/aaa.jpg, instead of/en/images/aaa.jpg.

In addition, MapPath can be used as follows: MapPath ("~ /Aaa.jpg "). The preceding solution does not process this special directory (~).

My solution is attached below:

Code
/// <Summary>
/// Reverse of MapPath
/// </Summary>
/// <Param name = "path"> File path in the disk </param>
/// <Returns> virtual path in current web application </returns>
Public static string PathMap (string path)
{
// Example 1:
// Path: c: \ websites \ piic \ en \ images \ aaa.jpg
// Application root: c: \ website \ piic \ en
// Virtual root :/
// Result:/en/images/aaa.jpg

// Example 2:
// Path: c: \ websites \ piic \ en \ images \ aaa.jpg
// Application root: c: \ website \ piic \ en
// Virtual root:/en/
// Result:/en/images/aaa.jpg

If (path. StartsWith ("~ /"))
{
Var virtualRoot = HostingEnvironment. ApplicationVirtualPath;
Path = virtualRoot + path. Substring (2 );
}

Var approot = HostingEnvironment. ApplicationPhysicalPath;

Return path. Replace (approot, string. Empty). Replace ('\\','/');
}

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.