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. :-)

About

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

Sponsors

Recent Tweets

  • @SaraJChipps StackOverflow has keyboard shortcuts? Is there a page that lists them out? 2 days ago
  • @shanselman I use keyboard shortcuts in gmail and reader. They're only useful on sites you visit frequently enough to bother learning them. 2 days ago
  • @jcroft 1 or 2 in general, but it's dependent on who my target audience is. 3 days ago
  • "Wide left". The two sweetest words in the English language. The Pats are going to the Super Bowl! 1 week ago
  • RIAA Reminds Us Why We Hate Them With Obnoxious Smartass Tweet http://t.co/SHhaijFC #fb 2 weeks ago