Created
January 6, 2022 23:27
-
-
Save floko84/5ce713e8b58693439772da7f5e44a8a8 to your computer and use it in GitHub Desktop.
Reverse of HttpServerUtility.MapPath or HostingEnvironment.MapPath, maps a physical path to an absolute virtual path.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
using System.Web.Hosting; | |
public static class VirtualPathHelper | |
{ | |
/// <summary> | |
/// Returns the virtual path that corresponds to the specified physical file path. | |
/// </summary> | |
/// <param name="path"></param> | |
/// <returns></returns> | |
public static string MapVirtualPath(string path) | |
{ | |
var relativePath = new Uri(HostingEnvironment.ApplicationPhysicalPath) | |
.MakeRelativeUri(new Uri(path)) | |
.ToString(); | |
var appPath = VirtualPathUtility.AppendTrailingSlash(HostingEnvironment.ApplicationVirtualPath); | |
return string.IsNullOrEmpty(relativePath) | |
? appPath | |
: VirtualPathUtility.Combine(appPath, relativePath); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment