Problem with Related Posts in BlogEngine.NET 1.4

With the new release of BlogEngine.NET a new feature was added that shows the description or part of the post when listing related posts. If the description is empty a small part of the post content is listed instead and here in lies the problem. The post content is simply clipped as a certain length without regard to any tags in the post. Let's say a related posts goes like this:

<p>With the new release of
<a href="">BlogEngine.NET</a> a new feature was added.</p>

Now, when the post is clipped it may end up cutting in the middle of the href:

<p>With the new release of <a href="http://www.dotnetblo...

And this may probably screw up the layout and content of the remainder of the page, besides producing an html that will not validate.


This issue has been discovered and fixed for a future release of BlogEngine.NET. However, if you, like me don't or can't wait that long here's a fix:

The problem is that the html tags are not closed so the simple solution is to strip the related post of all html. My first thought was to parse the post and close all open tags but when I thought about the actual intent of the related posts I realized that it is to provide a preview of the post content, not to show part of the actual post. So, by stripping all html tags all images and links are removed and only the text remains, which provides a really nice preview.

To implement the solution you only have to edit one line of code in the App_Code/Controls/RelatedPosts.cs file on row 139:

string content = post.Content;



string content = Utils.StripHtml(post.Content);

Or you can download the appended file and replace the one in the App_Code/Controls/ folder.

Download RelatedPosts.cs (3,8KB)

Comments (1) -

  • Andy McKay

    2014-02-09 01:25:45 | Reply

    I wish I had discovered your post sooner, spent hours tracking down the cause of apparently random layout screw ups after adding the related posts control in BlogEngine ver 2. The source of my problem was tags directly in the post description, the existing logic in the current version does not check for this, so you need to make sure and strip out HTML from that as well as the content clippings.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.