Alex Rivera | Logout

Best way to display default image if specified image file is not found?

Asked 2009-04-04T20:15:26.340
15

I've got your average e-Commerce app, I store ITEM_IMAGE_NAME in the database, and sometimes managers MISSPELL the image name.

To avoid "missing images" (red X in IE), every time I display the list of products, I check the server for the image related to the product, and if that file doesn't exist - I replace it with default image.

As far as i can tell this doesn't affect performance, but I was wondering if there are any alternatives to fix a "missing image" problem.

I'm using ASP.NET + C# (.NET 3.5)

Some code:

foreach (Item item in Items)
{
  string path = Path.Combine("~/images/", item.categoryImage);
  item.categoryImage = File.Exists(Server.MapPath(path)) ? item.categoryImage : GetImageDependingOnItemType();
}
Edit
Report

1 Answer

0

I think your way is pretty much OK. I'd do it in a function or do in .categoryImage accessor.

answered 2009-04-04T20:30:56.753

Your Answer