ASP.NET Developer. ALT.NET Supporter. Pragmatic Programmer. Published Writer.

BlogEngine.NET Most Popular Posts Extension Fix

After unpublishing a post that I had accidentally published I noticed that my blog was throwing object reference not found exceptions all over the place.  It turns out the Most Popular Posts Extension I installed has a minor bug in how it handles unpublished and deleted posts that were published long enough to have a counter file associated with it (the extension generates a counter file for each blog post and sticks them in the App_Data folder).  Here's the fix for the TopPosts function in TopPosts.ascx.vb:

string postID = countFile.Substring(countFile.LastIndexOf('\\') + 1); 

// Only add the post if it can be found and if it is still published
Post post = Post.GetPost(new Guid(postID.Remove(postID.Length - 4)));

if (post != null && post.IsPublished == true)
{
    Counter temp = new Counter(postID.Remove(postID.Length - 4), Int32.Parse(fileRdr.ReadLine()));
    list.Add(temp);
}
fileRdr.Close();

It's pretty straightforward.  I just added a check to make sure the post exists and is still published before adding it to the list of popular posts.  Otherwise, the GetPage function will throw an exception because the "post" object will be null.  I hope this comes in handy for anyone else using this extension! 

Enjoyed this post? Share it with others!

  • DotNetKicks
  • DZone
  • Reddit
  • HackerNews
  • StumbleUpon
  • del.icio.us

3 Comments to BlogEngine.NET Most Popular Posts Extension Fix

  1. November 17, 2008 at 11:30 am | Permalink

    Thanks

  2. December 21, 2008 at 10:20 am | Permalink

    Good one…I want to ask you one thing which BlogEngine.NET template are you using?

  3. December 21, 2008 at 7:17 pm | Permalink

    @Pankaj

    No template. The blog theme was just something I threw together in my spare time. :-)

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About

Kevin Pang is an ASP.NET developer and published writer with over 6 years of experience in the software industry.

Recent Tweets