Getting relative virtual path from physical path Getting relative virtual path from physical path asp.net asp.net

Getting relative virtual path from physical path


Maybe this question is what you're looking for.There they suggest:

String RelativePath = AbsolutePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);


    public static string MapPathReverse(string fullServerPath)    {                    return @"~\" + fullServerPath.Replace(HttpContext.Current.Request.PhysicalApplicationPath,String.Empty);    }


Request.ServerVariables["APPL_PHYSICAL_PATH"]

is fine, but not always. It is available only if there's a HTTP request.

On the other hand the call

HostingEnvironment.ApplicationPhysicalPath

is always available.