This may not be the best way, but it works.
// Here is your path String p = photosLocation + "whatever.jpg"; // Here is the page address String pa = Page.Request.Url.AbsoluteUri; // Take the page name String pn = Page.Request.Url.LocalPath; // Here is the server address String sa = pa.Replace(pn, ""); // Take the physical location of the page String pl = Page.Request.PhysicalPath; // Replace the backslash with slash in your path pl = pl.Replace("\\", "/"); p = p.Replace("\\", "/"); // Root path String rp = pl.Replace(pn, ""); // Take out same path String final = p.Replace(rp, ""); // So your picture address is String path = sa + final;
Edit: Good, someone is flagged as not useful. Some explanation: take the physical path of the current page, divide it into two parts: the server and directory (for example, c: \ inetpub \ whatever.com \ whatever) and the name of the page (for example / Whatever.aspx). The physical path of the image must contain the path to the server, so "subtract" them, leaving only the path of the image relative to the server (for example: \ design \ picture.jpg). Replace the backslash with a slash and add it to the server URL.
Biri Aug 19 '08 at 12:03 2008-08-19 12:03
source share