代码之家  ›  专栏  ›  技术社区  ›  PassionateDeveloper

图像中的图像URLASP.Net页码

  •  0
  • PassionateDeveloper  · 技术社区  · 14 年前

    我在visualstudio的文件夹中有一个默认图像。

    如何获取服务器路径以在运行时代码中查找映像?

    if (String.IsNullOrEmpty(entry.Picture))
    {
        //image1.ImageUrl = @"F:\Temp\Projects\IMEICHECK\IMEICHECK\Images\Design\New\no-image.gif";
    }
    else
        image1.ImageUrl = entry.Picture;
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   KP.    14 年前

    看起来您的图像路径可能不是网站或web应用程序的一部分。这不是好的做法。

    /Common/Images/no-image.gif

    appSettings 节,或者如果它只在一个地方使用,则作为代码后面的常量字符串。

    private const string defaultNoImagePath = "/Common/Images/no-image.gif";
    
    if (string.IsNullOrEmpty(entry.Picture))
    {
        image1.ImageUrl = defaultNoImagePath;
    }
    else
    {
        image1.ImageUrl = entry.Picture;
    }
    
        2
  •  0
  •   bugventure    14 年前

    Server.MapPath("~/Images/Design/New/no-image.gif") 将为您提供服务器上图像的绝对路径。但是,似乎您需要资源URL来用于页面上的图像元素。在这种情况下,您可能需要使用 Page.ResolveClientUrl("~/Images/Design/New/no-image.gif") 这将给你一个绝对的网址,你可以设置为 src 一个图像HTML元素。但更简单的是,您使用的是服务器端图像控件,因此传递相对图像url(“~/Images/Design/New/no”)-图片.gif)就足够了。