I am working on Windows store app for windows and tablet. I need to list the product.
I want to set one condition: If product image is not available on URL, then it should use default image.
Here is the code:
//get product list by service call and bind to productList.
foreach(product item in productList)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(item.image.ToString()));
bool imageexist = false;
using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
{
int imagelength = Convert.ToInt32(response.ContentLength);
if (imagelength > 0)
{
imageexist = true;
}
else
{
imageexist = false;
}
}
if (item.image == "N/A" || imageexist == false)
{
item.image = "Images/NoDataImages/ico-no-orders.png"; //default image
}
}
Here, product can be 100 or more. So, every time it call to check on server.
Is there any other way to check because it calls to check everytime? It consume times.
I have referred following links. But in all cases I need to call to check image exist everytime.
1- How can I check if an Image exists at http://someurl/myimage.jpg in C#/ASP.NET
2- asp.net check if imageURL exists
3- Test to see if an image exists in C#
Please suggest utilized way.