【问题标题】:How do I convert a file path to a URL in ASP.NET如何在 ASP.NET 中将文件路径转换为 ​​URL
【发布时间】:2010-09-06 04:07:24
【问题描述】:

基本上我有一些代码来检查特定目录以查看图像是否存在,如果存在,我想将图像的 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;
    }
}

【问题讨论】:

    标签: asp.net url image


    【解决方案1】:

    据我所知,没有办法做你想做的事;至少不是直接的。我会将photosLocation 存储为相对于应用程序的路径;例如:"~/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);
        }
    }
    

    【讨论】:

    • 我在任何 .Net 版本的文档中都找不到 HttpRequest 的 ResolveUrl 成员。你在使用 ASP.Net MVC 吗?
    • @Fredrik 正如 Jared 指出的 HttpRequest 对象没有此方法。它可以在 Page 对象或 Web 控件对象中找到。您能否编辑您的答案以反映这一点?
    【解决方案2】:

    我认为这应该可行。它可能在斜线处关闭。不确定它们是否需要。

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

    【讨论】:

      【解决方案3】:

      所有这些答案的问题在于它们没有考虑虚拟目录。

      考虑:

      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"
      

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

      【讨论】:

      • 存在安全问题。通常,一个 Web 应用程序不应该能够在其自己的 Web 根目录之外对服务器执行任何操作。如果这确实是您想要的,您可以通过查询 IIS 元数据库来实现,如下所示:stackoverflow.com/questions/4710187/…。但是您的网络应用程序需要以危险的高权限运行。如果两个 Web 应用程序需要交换文件,最好为它们实现服务。
      • 可以想象,映射可能不是唯一的。两个虚拟目录可以映射到同一个 NTFS 文件夹。
      【解决方案4】:

      获取 URL 的左侧部分:

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

      获取应用(网络)名称:

      ?HttpRuntime.AppDomainAppVirtualPath
      "/"
      

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

      【讨论】:

        【解决方案5】:

        这是我用的:

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

        【讨论】:

        • 谢谢,这是Server - public static string MapUrl(this HttpServerUtilityBase Server, string path)的扩展方法
        • 看来这个是正确的答案,更干净!请点赞
        【解决方案6】:

        这对我有用:

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

        【讨论】:

        • 优秀的答案!应该有更多的赞成票。更详细一点,你需要一个开关来处理AppDomainAppVirtualPath,因为如果网站在根目录下,VirtualPath会是一个空字符串,所以文件名前不需要斜杠,如果填写VirtualPath,你需要添加一个额外的斜线。 string sVPath = HttpRuntime.AppDomainAppVirtualPath; if (sVPath.Length > 0) sVPath += "/"; string sCSSURL = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + sVPath + "myImage.jpg";
        【解决方案7】:

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

        【讨论】:

          【解决方案8】:

          我接受了 Fredriks 的回答,因为它似乎以最少的努力解决了问题,但是 Request 对象似乎并没有包含 ResolveUrl 方法。 这可以通过 Page 对象或 Image 控件对象来访问:

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

          如果您像我一样使用静态类,另一种方法是使用 VirtualPathUtility:

          myImage.ImageUrl = VirtualPathUtility.ToAbsolute(photoURL);
          

          【讨论】:

            【解决方案9】:

            也许这不是最好的方法,但它确实有效。

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

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

            【讨论】:

              【解决方案10】:

              据我所知,没有一个函数可以做到这一点(也许您正在寻找 MapPath 的反函数?)。我很想知道这样的功能是否存在。在那之前,我只会获取 GetFiles 返回的文件名,删除路径,然后添加 URL 根目录。这可以通用。

              【讨论】:

                猜你喜欢
                • 2016-07-26
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-02-15
                • 2014-04-27
                • 2014-05-04
                • 2021-10-18
                • 1970-01-01
                相关资源
                最近更新 更多