How I got my blog posts to show up on the front page of my website

Topics: ASP.NET 2.0
Nov 11, 2012 at 5:33 PM
Edited Nov 11, 2012 at 6:05 PM

Hey everyone, I just recently started blogging and I just wrote my first blog regarding BlogEngine.NET. My website as a whole is on ASP.NET but uses VB as the code behind. I wanted my blog posts to show up on the front page, so this is how I did it

Because I wanted the blogs to show up by creation date, I created the following class

Imports System.IO
Imports Microsoft.VisualBasic

Public Class clsCompareFileInfo
    Implements IComparer
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
        Dim File1 As FileInfo
        Dim File2 As FileInfo

        File1 = DirectCast(x, FileInfo)
        File2 = DirectCast(y, FileInfo)

        Compare = DateTime.Compare(File1.CreationTime, File2.CreationTime)
    End Function
End Class

I have to give credit for this code from here. I changed the class to use creation time instead of last write date. I created a method called generateXMLblogfeed() and fired it on the page load. "blogCollection" is the name of my server control.

Sub generateXMLblogfeed()
      
        Dim dirinfo As DirectoryInfo
        Dim allFiles() As FileInfo
        dirinfo = New DirectoryInfo(Server.MapPath("/blog/App_Data/posts"))
        'returns an array of paths
        allFiles = dirinfo.GetFiles("*.xml")
        Array.Sort(allFiles, New clsCompareFileInfo())
        'this will make the newest shows up first
        Array.Reverse(allFiles)
        'scan through the array and grab all needed info        
        For Each fl As FileInfo In allFiles
          
            Dim xdom As XmlDocument
            xdom = New XmlDocument()
            'load the file in the array that we are on
            xdom.Load(fl.FullName)
            Dim deleted As Boolean
            deleted = xdom.SelectSingleNode("post/isdeleted/text()").InnerText
            If (deleted = False) Then
                Dim pubdate As String = (xdom.SelectSingleNode("post/pubDate/text()").InnerText)
                pubdate = Replace(pubdate, "-", "/")
                pubdate = pubdate.Substring(0, 10)
                pubdate += "/"
                blogCollection.Text += "<a href='./blog/post/" + pubdate + xdom.SelectSingleNode("post/slug/text()").InnerText + ".aspx'>"
                blogCollection.Text += "<div class='snippetHeaders'>" + xdom.SelectSingleNode("post/title/text()").InnerText + "</div>"
                blogCollection.Text += "</a>"
                blogCollection.Text += "<div class='snippetDate'>" + xdom.SelectSingleNode("post/pubDate/text()").InnerText + "</div>"
            End If



            
        Next

    End Sub

Since there's no permalink attribute in the XML files, I had to create a link to the post by grabbing the first 11 characters of the post date, add the slug onto the end and then finally add the .aspx extension to the end! Pretty slick right? Integration is seamless. I'm pretty happy with it :)