I do the following code to check if an image exists:
public bool DoesImageExist(string imageUrl)
{
bool exists = false;
try
{
HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(imageUrl);
request.Timeout = 5000;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
exists = (response.StatusCode == HttpStatusCode.OK);
}
}
catch
{
exists = false;
}
return exists;
}
This works fine and returns a 404 for images that don’t exist, however since I switched on customErrors in the web.config, it started returning 200 status ok, because the image it requests returns a 404 and redirects to the error handler page which returns a 200 status ok. Is there a way around this to prevent the customErrors page from returning a 200 ok or working for images?
I found a quick way around this for anyone with the same problem by putting this code into my generic handler page (which I set in customErrors)
This basically forces it to return a 404 for any jpeg/png images, however for all other errors it forces the generic page to be shown.