Recently I have spent some time implementing page providers for a customer. A couple of days ago I noticed that one of the page providers started to behave strange for one particular PageId. The PageReference for that page had RemoteSite set to null instead of the page providers ProviderKey. This means that the PageReference will point on a page in the LocalPageProvider rather than on my page in my custom page provider. After some investigation it was clear that this particular PageReference was equal to the PageReference of the page providers EntryPoint.
The problem seemed to be related to constructing the PageReference which was handled by the PageProviderBase.ConstructPageReference methods. Reflecting these methods with Reflector made it clear that PageReferences are constructed with RemoteSite set to ProviderKey only if PageId is not equal to EntryPoint.ID. The workaround is to construct the PageReference yourself and always set the ProviderKey part. Be careful though when you are constructing ParentLink for top-level pages because it in these cases is located on the LocalPageProvider.
This post was first published on EPiServer World, available here.