Hide unpublished links for visitor

We just migrated a system from a custom built CMS into EPiServer. If the editor added a link to an unpublished page in the old CMS, it was automatically unlinked for the visitor until the page was published. We tried to find a built in way of doing in this in EPiServer, but could not find one. So instead we created a custom HtmlHelper that did if for us.

The helper method uses Html Agility pack to parse the html code and find links, then we find the content in EPiServer and check if it is published or not. If it’s unpublished we remove the wrapping a-tag.

Bear in mind that the lookup if a page is published or not costs in performance. We’ve added caching on the lookup so it’s only the first lookup on a url that takes time. Another thing to consider in the code below is that we only check pages, and not other content.