如何将文件path转换为ASP.NET中的URL

基本上我有一些代码来检查一个特定的目录,看看是否有图像,如果是这样我想分配一个URL到图像的ImageControl。

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; } } 

据我所知,没有办法做你想做的事情; 至less不是直接的。 我photosLocation存储为相对于应用程序的path; 例如: "~/Images/" 。 这样,您可以使用MapPath来获取物理位置,并使用ResolveUrl来获取URL(从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); } } 

这是我使用的:

 private string MapURL(string path) { string appPath = Server.MapPath("/").ToLower(); return string.Format("/{0}", path.ToLower().Replace(appPath, "").Replace(@"\", "/")); } 

所有这些答案的问题是,他们不考虑虚拟目录。

考虑:

 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 

所以:

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

但是没有办法做到这一点:

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

因为没有办法获得完整的虚拟目录列表。

我已经接受Fredriks的答案,因为它看起来用最less的努力来解决问题,但是Request对象似乎并没有考虑ResolveUrl方法。 这可以通过Page对象或Image控件对象来访问:

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

另一种方法是,如果您正在使用静态类,则使用VirtualPathUtility:

 myImage.ImageUrl = VirtualPathUtility.ToAbsolute(photoURL); 

也许这不是最好的方法,但它的工作原理。

 // 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's address is String path = sa + final; 

编辑:好的,有人标记为没有帮助。 一些解释:取当前页面的物理path,将其分成两部分:服务器和目录(如c:\ inetpub \ whatever.com \ whatever)和页面名称(如/Whatever.aspx)。 图像的物理path应该包含服务器的path,所以“减去”它们,只留下相对于服务器的图像path(如:\ design \ picture.jpg)。 用反斜杠replace反斜杠,并将其附加到服务器的URL。

这对我工作:

 HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + HttpRuntime.AppDomainAppVirtualPath + "ImageName"; 

据我所知,没有一个单一的函数可以做到这一点(也许你正在寻找MapPath的逆向函数 ?)。 我很想知道这样的function是否存在。 在此之前,我只需要GetFiles返回的文件名,删除path,并预先安装URL根目录。 这可以做到一般。

简单的解决scheme似乎是在网站中有一个临时的位置,您可以通过URL轻松访问,然后在需要保存时可以将文件移动到物理位置。

要获取url的左侧部分:

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

获取应用程序(networking)名称:

 ?HttpRuntime.AppDomainAppVirtualPath "/" 

有了这个,你可以在获得完整的URL之后添加你的相对path。

我认为这应该工作。 它可能是斜线。 不知道他们是否需要。

 string url = Request.ApplicationPath + "/" + photosLocation + "/" + files[0];