Reputation: 4667
Is there a way to gracefully check whether a page exists in EpiServer CMS 5 (given a pageId integer) without having to catch the PageNotFoundException
thrown by
DataFactory.Instance.GetPage(pageReference)
(EpiServer will happily create a PageReference using a non existing pageId).
Surely I can check whether a page exists without throwing an exception or doing a massive loop?
Upvotes: 3
Views: 2803
Reputation: 10350
IContentLoader
already has the TryGet
method with a few overloads:
bool TryGet<T>(ContentReference contentLink, out T content) where T : IContentData;
bool TryGet<T>(ContentReference contentLink, CultureInfo language, out T content) where T : IContentData;
bool TryGet<T>(ContentReference contentLink, LoaderOptions settings, out T content) where T : IContentData;
bool TryGet<T>(Guid contentGuid, out T content) where T : IContentData;
bool TryGet<T>(Guid contentGuid, CultureInfo language, out T content) where T : IContentData;
bool TryGet<T>(Guid contentGuid, LoaderOptions loaderOptions, out T content) where T : IContentData;
Try to get with the page reference and check the Boolean result. Also, it makes sense to check whether the content
is a proper reference - via ContentReference.IsNullOrEmpty
.
Upvotes: 0
Reputation: 1086
I find it nice to do the catching in an extension method:
public static bool TryGetPage(this PageReference pageReference, out PageData pd)
{
try
{
pd = DataFactory.Instance.GetPage(pageReference);
return true;
}
catch (Exception)
{
pd = null;
return false;
}
}
Upvotes: 0
Reputation: 753
There is a static method of PageReference which should help:
PageReference.IsNullOrEmpty(pageLink)
Upvotes: -3
Reputation: 2375
[EPiServer CMS 5 R2 SP2] No, not without bypassing the page cache and that is more expensive than catching the exception.
Upvotes: 6