Check broken link of in server side and client side

Server side using C# code:

NOTE: using System.Net;

Uri urlCheck = new Uri("http://dev.test.com/_layouts/images/test_img.jpg");
HttpWebReqeust request = (HttpWebRequest)WebRequest.Create(urlCheck);
request.UseDefaultCredentials = true;
request.PreAuthenticate = true;
request.Credentials = CredentialCache.DefaultCredentials;

try
{
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    imgTag.ImageUrl = "http://dev.test.com/_layouts/images/test_img.jpg";
}
catch(Exception)
{
    imgTag.ImageUrl = "/_layouts/images/error_img.png";
}

Client side using JQuery:

$(document).ready(function() {
     $("img[id=imgUserPhoto]").error(function() {
          $(this).attr("src", "/_layouts/images/error_img.png");
     });
});
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s