Digital Colony!

Creating an ASP.NET Blog Using Blogger

When I relaunched Digital Colony in January 2007, I made the decision to do it using Blogger. Prior to that this site used a publisher I wrote myself in Classic ASP. It did a fine job for years, but it didn't have comments or tags. Also, I've been using Blogger since April 2000, so I have a high level of comfort with it's interface.

Blogger Accounts

Before proceeding, I believe it's important to explain there are 3 levels of accounts that can use Blogger.
  1. BlogSpot Hosted - xxxx.blogspot.com
  2. Blogger Hosting Domain - your xxxx.com site is hosted on their servers.
  3. FTP Account - you manage your server and Blogger FTPs built pages to your site.
If you want to use ASP.NET, you will have use option #3. This means you will need your own disk space and FTP access. From this point forward, everything discussed applies only to Blogger accounts accessed via FTP.

Why Blogger? The Good Stuff

What I like about Blogger is you can use any file extension you like. This means I can use ASP.NET 2.0 and use Master Pages. I can also drop server controls onto my template. And I like the fact the code base resides on Google's server, not mine. If they need to update the publisher, they can without me having to download and install anything. Most blog tools require the user to install patches and perform upgrades.

The 2006 release of Blogger gave us tags. Blogger calls them labels, but it's the same concept. You can create an unlimited number of tags to quickly locate posts. Here is a link to all my posts with the tag Hexadecimal.

Things I Dislike About Blogger

Before you jump headfirst into using Blogger, let me make you aware of some of the bad things. As of July 2007, many of the Blogger markup tags do not generate XHTML compliant code. Prior to 2006 upgrade, all Blogger tags generated valid XHTML. I've brought this error to their attention numerous times but they have never emailed me back. For the most part Blogger no longer cares about their customers that use the FTP option.

This brings me to the second thing I dislike about Blogger. Their support is somewhere between awful and non-existent. It's July 2007 and the site still has some bugs that should have been fixed by now. Other publishers have bugs as well. But other publishers aren't owned by Google, which has a market cap of $168 Billion at this moment.

WordPress, SubText, Community Server?

Before I get a flood of emails, let me explain why I picked Blogger over other blog publishing tools. I made my decision to go with Blogger in December 2006. Some of my reasons have been addressed since then or will be soon.
  • WordPress - WordPress is the best blog tool. However it was coded for Apache using PHP. And although you can run it on a .NET server using IIS, you won't be able to use Master Pages or ASP.NET server controls. If you need a blog and you don't need .NET, use WordPress.
  • SubText - Unlike Blogger it has trackbacks, but it doesn't do tags the way I like. When you click on a tag on this site it goes to more posts on that topic on THIS site. Subtext handles tags by sending your readers off your site and over to Technorati. That was unacceptable for my needs. This project continues to get better. At some point I predict it will become the best .NET publisher.
  • Community Server - I downloaded and tested Community Server. It was a beast. Huge install, slow and full of errors. As a community builder, it may be tweaked to run fine. As a simple blog publisher it was lousy.
  • Other .NET Blog tools - I tested a few other .NET blog tools. Anything that was written using ASP.NET 1.x or didn't have tags was eliminated from consideration. Master Pages were introduced in ASP.NET 2.0, so using 1.x was not an option for me.
  • weblogs.asp.net - Microsoft offers a way for developers to post their blogs on their server. How nice of them. No thanks. DigitalColony.com is my asset and I will spend my time creating value for my domain, not theirs. If design frightens you and you are too cheap to buy a domain and pay for hosting, consider using them. Some of the best ASP.NET bloggers use them.

Overview on Creating an ASP.NET Blog on Blogger

  1. Sign up for a Blogger account.
  2. From the Blogger Dashboard, click the link that says Create a New Blog
  3. Skip the top of this screen and follow the link for Advanced Setup
  4. In addition to completing all the required information, make sure your blog filename is default.aspx.
  5. The next screen forces you to pick an ugly template. Hold your nose and pick one. You can change it later.
  6. From the settings tab, select the Archive link. Change the Archive Filename to archive.aspx.
  7. Create and publish dummy blog post. The post should have one label.
  8. NOTE: Blogger is not expecting the .ASPX file extension and will often post the first blog as .HTML regardless of how you set it up. Delete the dummy post.
  9. Create and publish the second dummy blog post. If you have all your paths correct, it should now be working. Test the home page, the post page, the archive page and the label page. If all is working well, you can delete dummy post number 2. You now have a working .NET Blog running on Blogger.

Master Pages and Getting Rid of the Ugly Template

Some of you will be happy with the template you selected. I bid you farewell at this point in the article. For the rest of us, the goal is now to setup a Master Page and make a nicer template.

Use Visual Studio to create your Master Page and Theme. Then when you are happy, go back to Blogger. From the Template tag, you are now going to step into the Edit HTML screen. Mixing Blogger markup and ASP.NET markup without the benefit of Intellisense or a debugger is tough. The process could take hours. Always backup changes to Notepad as you go.

Cool Looking Code and Extending Blogger

To get cool looking sample code, I use the Manoli.net formatter. And because your blog is in ASP.NET, you can add namespaces or create custom controls. A while back I wrote Blogger Label List for FTP Accounts (ASP.NET), which creates the control in the right column that displays all the tags and number of posts for that tag.

Last Thought

Some of you will give up and decide to use another blog publishing tool or live with one of the ugly templates Blogger provides. It all depends on what is important to you. Digital Colony uses Master Pages, the Yahoo! UI library for positioning, ASP.NET Themes, Imports a custom Namespace and has server-side controls in the template. All that is possible using Blogger.

Labels: ,

 

Blogger Label List for FTP Accounts (Classic ASP)

For an overview on Blogger Label Lists read Blogger Label List for FTP Accounts (ASP.NET). Below is the code used to create a Label List using Classic ASP with VBScript.

Modify the first line to point to your label folder. Then correct the spelling of bl0gger-labels. It is purposely misspelled as to not throw off the count on this page. Save this code with a .asp file extension and then use a server-side include to place in onto your page template.
'-- add your label directory here
labelDir = Server.MapPath("/myblog/labels/") 
'-- Check for Directory
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(labelDir) Then
    Response.Write "<ul>"
    Set labelFolder = FSO.GetFolder(labelDir)
    Set labelBlogs = labelFolder.Files
    For each label in labelBlogs
        Set labelFile = FSO.GetFile(label)
        Set labelStream = labelFile.OpenAsTextStream (1, -2)    
        iLabelCount = 0
        '-- Read the file line by line
        Do While Not labelStream.AtEndOfStream                
            Line = labelStream.readline
            LineCount = Sgn(InStr(Line,"bl0gger-labels"))                
            iLabelCount = iLabelCount + LineCount
        Loop                                
        Response.Write "<li>" + Replace(labelFile.Name,".asp","") & " (" + CSTR(iLabelCount) & ")</li>"
        Set labelStream = nothing
        Set labelFile = nothing
    Next
    Response.Write "</ul>"
Else
    Response.Write "<p>No labels in folder: " & labelDir & "</p>"
End If
Set FSO = nothing    
Below is an example of using a server-side include inside a Classic ASP page.
<div id="divLabelList">
<!--#include virtual="/inc/theme.asp"-->
</div>
Back in the day, Ev would have been proud of me.

Labels: , ,

 

Blogger Label List for FTP Accounts (ASP.NET)

The new version of Blogger includes tags. They call them Labels. Hosted accounts (BlogSpot) get templates which display a list of tags for quick navigation. This feature is not available to FTP accounts. This post is a hack to create a user control in ASP.NET that displays a Label List. Before I do, lets breakdown how labels look on the file system.
  1. Labels reside in a folder. By default that folder is called labels.
  2. Every label gets one file.
  3. The label file name is the same as the label name plus extension.
  4. Inside each file is a copy of each blog with that label.
  5. Blogger places the class name of blogger-labels before the Labels text.
For example, on this site I have a label called Javascript.
http://digitalcolony.com/labels/Javascript.aspx
It resides in the labels folder just outside the blog root. The name of the label is Javascript. The name of the file is Javascript.aspx. As of this writing there is 1 post and therefore 1 instance of blogger-labels in the View Source. With the above facts laid out, this is an overview of how to create your own Label List control.
  1. Using server-side code to handle Files and Directories, open the labels folder.
  2. Collect each file name (minus the file extension).
  3. Open each file and count how many times blogger -labels appears
  4. Write label and count to screen.
Here is my .NET code for the Blogger Label List control. On the .ASPX page that the control will be on add this line to the top to register the control.
<%@ Register Src="~/LabelList.ascx" TagName="LabelList" TagPrefix="dc" %>
And the control on the page gets placed with this line.
<dc:LabelList ID="LabelList1" runat="server" Folder="labels" DisplayCount="true" />
And here is the code for the LabelList.ascx user control.
using System;
using System.Web;
using System.IO;
using System.Text;

public partial class LabelList : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {       
        string rootDir = Server.MapPath("~");
        string labelDirectory = rootDir + folder;
        StringBuilder sb = new StringBuilder();
        if(Directory.Exists(labelDirectory)){
            
            string[] fileList = Directory.GetFiles(labelDirectory,"*.aspx");
            string labelName;
            string labelCount;

            foreach (string file in fileList)
            {
                FileInfo fi = new FileInfo(file);
                labelName = fi.Name.Replace(fi.Extension, "");

                if (displayCount)
                {
                    StreamReader sr = new StreamReader(fi.FullName);
                    string webPage = sr.ReadToEnd();
                    sr.Close();
                    int webPageLen = webPage.Length;
                    webPage = webPage.Replace("bl0gger-labels", "bl0gger-labelsX");
                    int iLabelCount = webPage.Length - webPageLen;
                    labelCount = "(" + iLabelCount.ToString() + ")";
                }
                else
                {
                    labelCount = "";
                }
                folder = folder.Replace("\\","/");
                if (folder.Substring(folder.Length - 1, 1) == "/")
                    folder = folder.Substring(0, folder.Length - 1);
                sb.Append("<a href=\"" + folder + "/" + fi.Name + "\">" + 
labelName + labelCount + "</a><br/>");               
            }            
        } 
        else {
            sb.Append("No labels in folder [" + labelDirectory + "] defined.");
        }
        Response.Write(sb.ToString());
    }

    private string folder;
    public string Folder
    {
        get { return folder; }
        set {
            if (value.Substring(0, 1) == "/")
                folder = value;
            else
                folder = "/" + value;
            folder = folder.Replace("/","\\");
        }
    }
    private bool displayCount;
    public bool DisplayCount
    {
        get { return displayCount; }
        set { displayCount = value; }
    }
}
Note that the code above misspells blogger-labels as to not confuse the count of the Label control that I'm using for this site. If you cut and paste the code, fix that spelling.

PHP and ASP coders should be able to hack up something similar using the above code as a framework.

UPDATE (Feb 2007): I created a version of Blogger Label Lists using Classic ASP.

Labels: ,

 

Digital Colony Copyright © 1999-2008 XHTML   508
This site uses Blogger, which is not 100% XHTML compliant.
Try...Catch Disclaimer: For brevity many examples do not include error handling. That is your responsibility.