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 ('\\','/');
}