Help shape the future of CMS PaaS release notes! Take this quick survey and share your feedback. 

Deane Barker
Sep 3, 2010
  6040
(2 votes)

Correcting All Raw URLs Inside a String of HTML

If you create a link inside an EPiServer page to another EPiServer page, the link that gets embedded in the HTML is not friendly.  It looks something like this:

/Templates/PageTypes/TextPage.aspx?id=66

EPiServer corrects these links very late in the page lifecycle – actually in a response filter after the entire page has been generated.  This way, it’s sure to correct links no matter where on the page they might be.

What I noticed, however, is that these links were only getting corrected for Web forms.  More specifically, they were only getting corrected for anything with a “text/html” content type.

This was usually fine because I could change the response type, but sometimes it was an issue, especially when doing Ajax callbacks.  You often send those back as some plain text format like JSON, and you form the response at the code level, concatening the raw values of properties into a string.  Before long, you notice that your links aren’t corrected in blocks of HTML.

It’s not hard to get the friendly URL from a PageData object:

UrlBuilder url = new UrlBuilder(pd.LinkURL);
UrlRewriteProvider.ConvertToExternal(url, somePageData.PageLink, UTF8Encoding.UTF8);
string theFriendlyURl = url.ToString();

This is great to go from a PageData object to a friendly URL.  What I found to be much tricker, however, was if I had a big string of HTML with a bunch of embedded links and I needed to correct them all before working with the string.

When I was in Sweden for the Partner Summit, I posed this question to Magnus Stråle.  I was awfully grateful when he took the time to dig through EPiServer’s unit tests to find a chunk of code for me.  It’s distilled down to this function:

private string CorrectLinks(string rawHtml)
{
    var toExternal = new FriendlyHtmlRewriteToExternal(UrlBuilder.RebaseKind.ToRootRelative);
    return toExternal.RewriteString(
        new UrlBuilder(HttpContext.Current.Request.Path),
        new UrlBuilder(HttpContext.Current.Request.RawUrl),
        HttpContext.Current.Response.ContentEncoding,
        rawHtml);
}

This will take a string of HTML, correct all the links inside of it, and return another string, ready for insertion into any response, be it text, JSON, or whatever.

Thanks to Magnus for taking the time to help me solve this.

Sep 03, 2010

Comments

Mari Jørgensen
Mari Jørgensen Sep 21, 2010 10:33 AM

Nice one!

Sep 21, 2010 10:33 AM

Nice. :)

Alex Judd
Alex Judd Jan 31, 2011 07:49 PM

Interestingly this caused absolute chaos in a situation where we _did_ want to pass parameters using the URL string (GET parameters) as the page renderer tried to read our ?parameter=value set as well and use it as a page ID.

To get around this [trial and error found this out], add your own parameters with the ¶meter=value as the pageID will be by default the first parameter and want to use the ?parameter=value tag, and then EPiServer converts this to a ?parameter=tag automatically once it has tidied up the URLs in the page.

Very clever when you know how!

Please login to comment.
Latest blogs
Be careful with your (order) notes

This happened a quite ago but only now I have had time to write about it. Once upon a time, I was asked to look into a customer database (in a big...

Quan Mai | Feb 5, 2025 | Syndicated blog

Imagevault download picker

This open source extension enables you to download images as ImageData with ContentReference from the ImageVault picker. It serves as an alternativ...

Luc Gosso (MVP) | Feb 4, 2025 | Syndicated blog

Optimizely SaaS vs PaaS: A Comparison from Client and Developer Perspectives

Optimizely, one of the leading digital experience platform. Offering both Software-as-a-Service (SaaS) and Platform-as-a-Service (PaaS) solutions....

Praful Jangid | Feb 3, 2025

Returning to Optimizely After Many Years

Returning to Optimizely After Many Years: A Journey Through Its New Features After several years away from Optimizely’s Content Management … More

Jose Neto | Feb 2, 2025 | Syndicated blog

Aspiring Optimizely

Adding .NET Aspire application host to your Optimizely project (even if it’s just for local development) is really a time-saver when it comes to...

valdis | Feb 2, 2025 | Syndicated blog

My Journey to Optimizely Certification: Study Tips, AI Tools, and Exam Strategies

How I Prepared for My Optimizely Certification Exam Hello everyone, I recently took the Optimizely Certification Developer Exam and became certifie...

calimat | Jan 31, 2025