title: Load default image if image is missing
body: <p>In my asp.net website, I am building certain pages dynamically.  There are certain images that I load from my server.  If the image is not present, then I need to load a default image instead.</p>
  
  <p>Until now I've been checking to see if the url is valid, and if it is, then I know the image is present.  If the url is invalid, then I know, in my code, to supply my default image.  To that end, I've done this:</p>
  
  <pre><code>    //returns true if the url actually exists
      //however, this will ALWAYS throw an exception if it exists, so beware the debugger.
      public static bool IsValidUrl(string url) {
          HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url);
          httpReq.AllowAutoRedirect = false;
          httpReq.Method = "HEAD";
          httpReq.KeepAlive = false;
          httpReq.Timeout = 2000;
          HttpWebResponse httpRes = default(HttpWebResponse);
          bool exists = false;
          try {
              httpRes = (HttpWebResponse)httpReq.GetResponse();
              if (httpRes.StatusCode == HttpStatusCode.OK) {
                  exists = true;
              }
          } catch {
          }
          return exists;
      }
  </code></pre>
  
  <p>But that isn't really a good way to do things, and I don't like having to proof against the exception like that.  Also, if I add new images, the server doesn't think the new images are valid urls until a certain time has passed (or I restart the website in IIS) - this is the bugger that has led me to look for another method.</p>
  
  <p>Is there a better way to supply a default image that will display in case my chosen image is not present?</p>