开发者

Image URL in a ASP.Net Page

I have a default image in my folder in visual studio.

How can I get the server path开发者_如何转开发 to find the image in runtime in code?

if (String.IsNullOrEmpty(entry.Picture))
{
    //image1.ImageUrl = @"F:\Temp\Projects\IMEICHECK\IMEICHECK\Images\Design\New\no-image.gif";
}
else
    image1.ImageUrl = entry.Picture;


It looks as though perhaps your image path is not part of the website or web application. This is not good practice.

You should put the image in a common location within the web app/site, such as:

/Common/Images/no-image.gif

And then easily store that path either in the web.config appSettings section, or as a constant string in your code-behind if it's only used in one place.

private const string defaultNoImagePath = "/Common/Images/no-image.gif";

if (string.IsNullOrEmpty(entry.Picture))
{
    image1.ImageUrl = defaultNoImagePath;
}
else
{
    image1.ImageUrl = entry.Picture;
}


Server.MapPath("~/Images/Design/New/no-image.gif") would give you the absolute path of the image on the server. However, it seems you need the resource URL to use for an image element on your page. In this case, you may want to use Page.ResolveClientUrl("~/Images/Design/New/no-image.gif") which will give you an absolute URL you could set as the src of an image HTML element. But even simpler, you are using a server-side Image control, so passing the relative image url ("~/Images/Design/New/no-image.gif") would suffice.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜