How to get server path of physical path ?

Freshblood picture Freshblood · Jun 22, 2011 · Viewed 10.2k times · Source

I want to convert this physical path "C:\bla\bla\Content\Upload\image.jpg" to server path like "/Content/Upload/image.jpg".

How can i do that ?

Answer

Tomasz Jaskuλa picture Tomasz Jaskuλa · Jun 22, 2011

you can use something like that :

 public static class Extensions        {
        public static string RelativePath(this HttpServerUtility utility, string path, HttpRequest context)
        {
            return path.Replace(context.ServerVariables["APPL_PHYSICAL_PATH"], "/").Replace(@"\", "/");
        }
    }

and you call

Server.RelativePath(path, Request);