Hi, In this post let me explain you about how to verify if  the remote files exist or not in ASP.NET 5.  Sometimes we need to verify if a file exists remotely such as javascript or image file.  Suppose you are in server(xxx.com) and you want to check a file in another server(xxxxxx.com) - in this case it will be helpful. And now, write the following code snippet.

Using HTTPWebRequest:
private bool RemoteFileExistsUsingHTTP(string url)
{
try
{
 //Creating the HttpWebRequest
 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
 //Setting the Request method HEAD, you can also use GET too.
  request.Method = "HEAD";

  //Getting the Web Response
  HttpWebResponse response = request.GetResponse() as HttpWebResponse;

  //Returns TURE if the Status code == 200
  return (response.StatusCode == HttpStatusCode.OK);
}
catch
{
//Any exception will returns false.
return false;
}
}


Using WebClient:
private bool RemoteFileExistsUsingClient(string url)
{
bool result = false;
using (WebClient client = new WebClient())
{
try
{
    Stream stream = client.OpenRead(url);
    if (stream != null)
    {
           result = true;
    }
    else
    {
           result = false;
    }
}
catch
{
 result = false;
}
}
return result;
}

Call Method:
RemoteFileExistsUsingHTTP("http://localhost:16868/JavaScript1.js");

Don't confuse here as a result of it absolutely was implemented in 2 ways. continually use HttpWebRequest class over WebClient because of the following reasons:
1. WebClient internally calls HttpWebRequest
2. HttpWebRequest has a lot of options (credential, method) as compared to Webclient

HostForLIFE.eu ASP.NET 5 Hosting
HostForLIFE.eu is European Windows Hosting Provider which focuses on Windows Platform only. We deliver on-demand hosting solutions including Shared hosting, Reseller Hosting, Cloud Hosting, Dedicated Servers, and IT as a Service for companies of all sizes. We have customers from around the globe, spread across every continent. We serve the hosting needs of the business and professional, government and nonprofit, entertainment and personal use market segments.