Hack: Getting the html from a PageData in EPiServer CMS5 Part2
Ok, as you found out if you tried to use the html that was generated using the code in the previous post, you'd notice that all links were rendered in the good ol "classic" style: "templates/page.aspx?id=3&language=en"
So, how can we fix that when our "rendering" doesnt pass through the ResponseFilter that performs the conversion of urls into their friendly form?
Well, one way would be to call the appropriate conversion function ourselves, principally in the same way EPiServer itself does.
And, in fact its only a few lines of code thats needed:
(Note that you need to bring in the namespace "EPiServer.Web" if you havent done so already).
1: // now, use the HtmlRewriter to jit-fix links to friendly urls
2: UrlBuilder UrlExternal = new UrlBuilder(pageRelativeUrl);
3: UrlBuilder UrlInternal = new UrlBuilder(pageRelativeUrl);
4:
5: Global.UrlRewriteProvider.ConvertToExternal(UrlExternal, pageData.PageLink, System.Text.Encoding.UTF8);
6:
7: HtmlRewriteToExternal htmlRewriter = Global.UrlRewriteProvider.GetHtmlRewriter();
8: capturedHtml = htmlRewriter.RewriteString(UrlInternal, UrlExternal, System.Text.Encoding.UTF8, capturedHtml);
And, voila, all links are now extremely friendly as is the <form action=="">-tag.
An additional note:
If you want to use the html "outside" of the web application context (for example if you decide to save the html to a local .html file or if you use the html in the body of a mail), you will have to "inject" an appropriate <base href="">-tag so that all relative url's work - if not, you'll se lots of broken links and javascript-errors.
If I get the time and energy, I might blog about that in a future post.
Regards,
Johan Olofsson
18 June 2008