1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Determine if a URL is valid via the API

Determine if a URL is valid via the API

2 posts, 0 answered
  1. Dave
    Dave avatar
    29 posts
    Registered:
    31 Oct 2011
    18 Oct 2012
    Link to this post
    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?
  2. Patrick Dunn
    Patrick Dunn avatar
    237 posts
    Registered:
    03 Nov 2014
    19 Oct 2012
    Link to this post
    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
2 posts, 0 answered