How to convert a path to a URL in ASP.NET

Basically, I have some code to check a specific directory to see if there is an image, and if so, I want to assign a URL to the ImageControl image.

if (System.IO.Directory.Exists(photosLocation)) { string[] files = System.IO.Directory.GetFiles(photosLocation, "*.jpg"); if (files.Length > 0) { // TODO: return the url of the first file found; } } 
+41
url image
Aug 19 '08 at 11:24
source share
10 answers

As far as I know, there is no way to do what you want; at least not directly. I would save photosLocation as a path relative to the application; for example: "~/Images/" . So you can use MapPath to get the physical location and ResolveUrl to get the url (with a little help from System.IO.Path ):

 string photosLocationPath = HttpContext.Current.Server.MapPath(photosLocation); if (Directory.Exists(photosLocationPath)) { string[] files = Directory.GetFiles(photosLocationPath, "*.jpg"); if (files.Length > 0) { string filenameRelative = photosLocation + Path.GetFilename(files[0]) return Page.ResolveUrl(filenameRelative); } } 
+13
Aug 19 '08 at 12:02
source share

this is what i use:

 private string MapURL(string path) { string appPath = Server.MapPath("/").ToLower(); return string.Format("/{0}", path.ToLower().Replace(appPath, "").Replace(@"\", "/")); } 
+10
Oct 15 '12 at 14:17
source share

The problem with all of these answers is that they do not account for virtual directories.

Consider:

 Site named "tempuri.com/" rooted at c:\domains\site virtual directory "~/files" at c:\data\files virtual directory "~/files/vip" at c:\data\VIPcust\files 

So:

 Server.MapPath("~/files/vip/readme.txt") = "c:\data\VIPcust\files\readme.txt" 

But do not do this:

 MagicResolve("c:\data\VIPcust\files\readme.txt") = "http://tempuri.com/files/vip/readme.txt" 

because there is no way to get a complete list of virtual directories.

+10
Oct. 27 '12 at 15:00
source share

I made a decision by Fredriks because it seems to solve the problem with the least amount of effort, however the Request object does not seem to be conatin by the ResolveUrl method. Access to this object is possible through the Page object or Image control object:

 myImage.ImageUrl = Page.ResolveUrl(photoURL); myImage.ImageUrl = myImage.ResolveUrl(photoURL); 

An alternative if you use a static class like me is to use VirtualPathUtility:

 myImage.ImageUrl = VirtualPathUtility.ToAbsolute(photoURL); 
+8
Aug 19 '08 at 13:51
source share

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.

+3
Aug 19 '08 at 12:03
source share

This worked for me:

 HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath + "ImageName"; 
+1
Jan 24 2018-12-12T00:
source share

As far as I know, there is not a single function that does this (perhaps you were looking for a reverse MapPath ?). I would like to know if such a function exists. Until then, I would just take the file name returned by GetFiles, delete the path and add the root URL. This can be done in the general case.

0
Aug 19 '08 at 11:53
source share

A simple solution is to have a temporary location on a website that you can easily access using a URL, and then you can move files to a physical location when you need to save them.

0
Jul 29 '11 at 11:40
source share

To get the left side of the URL:

 ?HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) "http://localhost:1714" 

To get the name of the application (network):

 ?HttpRuntime.AppDomainAppVirtualPath "/" 

With this, you can add your relative path after receiving the full URL.

0
Nov 19 '12 at 11:12
source share

I think this should work. This can be disabled. Not sure if they are needed or not.

 string url = Request.ApplicationPath + "/" + photosLocation + "/" + files[0]; 
-2
Aug 19 '08 at 11:55
source share



All Articles