Determine if a URL is valid via the API

Posted by Community Admin on 04-Aug-2018 19:11

Determine if a URL is valid via the API

All Replies

Posted by Community Admin on 18-Oct-2012 00:00

I need to be able to query SF to determine if a URL is valid.

For instance, if I receive a request for "~/home/this-part-is-invalid/", I need to be able to check the path against the pages stored in SF to determine if it will return a valid page, or a 404.

How do I do this?

Posted by Community Admin on 19-Oct-2012 00:00

Hi Dave,

 You could use a HttpWebRequest to get the URL and check if it is valid or not. What kind of functionality are you trying to implement? Is this for a widget or for a specific page? If you let me know I can give you better direction on where to drop this and how to build it out but here's the basics of URL validation:

using System.Net;
 
///
/// Checks the file exists or not.
///
/// The URL of the remote file.
/// True : If the file exits, False if file not exists
private bool RemoteFileExists(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;
    

Let me know if this doesn't answer your question! Regards,
Patrick Dunn
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed