Best way to check if a page exist?

Vote:
 

Hi,

I just wounder, which is the best way to verify that a page really exist?
I dont want to use try catch, and catch the pagenotfound exception.

PageReference pr = new PageReference(PageId);

try
{
      PageData pd = DataFactory.Instance.GetPage(pr);
}
catch
{
 throw...
}

#38268
Apr 07, 2010 8:55
Vote:
 

This should be a safe way:

PageData pd;
            PageReference pr = new PageReference(PageId);            
            
            if(!PageReference.IsNullOrEmpty(pr)) 
                pd = DataFactory.Instance.GetPage(pr);
            if (pd != null)
            {
                // Your page exists and you have access to it
            }

PageData pd;            
PageReference pr = new PageReference(PageId);                                    
if(!PageReference.IsNullOrEmpty(pr))                 
     pd = DataFactory.Instance.GetPage(pr);

if (pd != null)          
 {                
       // Your page exists and you have access to it          
 }

#38272
Edited, Apr 07, 2010 9:19
Vote:
 

Hi,
Thank you for your respond,
I think that new PageReference create a new PageReference even if the page dosent exist.
And that mean PageReference.IsNullOrEmpty is not going to be NullOrEmpty becase the Reference exist but is not a valid page.

                if(PageReference.IsNullOrEmpty(pr))
                    return;

                try
                {

                    // I GOT A PAGE NOTFOUND exception here.
                    PageData pd = DataFactory.Instance.GetPage(pr);
                    pageList.Add(pd);                              
                }
                catch
                {
                }

#38276
Apr 07, 2010 10:13
Vote:
 

There is one way to do it without the try catch, but I am not sure how supported it is.

 

you can use:

if(PageCoreData.Load(PageRef) != null)

{

//The pageRef is ok and exists.

}

PageCoreData.Load returns null if you have a page ref that leads to a page that does not exist instead of throwing an exception. I have not investigated it much though.. Like if it is slow /cached, ..., ... so might be some issues in some cases im sure.

#38278
Apr 07, 2010 10:43
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.