Using MetaWeblog API and Windows Live Writer to Manage Dynamic Website Content
I built the Miles Ahead Farm Website a few years ago. I built it for a friend of ours with the intention that my wife would manage the content. I built some administrative functionality using the FreeTextBox control as a rich content editor. It worked out great as it allowed Amy to very easily make textual and formatting changes without much hassle. The problem was that any time she wanted to upload images (which was often), I would still have to format, upload and insert the image. It wasn't a painful process, but I'm always looking for ways to make my life a little easier.
I then came across Windows Live Writer.
"Windows Live Writer is a desktop application that makes it easier to compose compelling blog posts using Windows Live Spaces or your current blog service. "
It has a few very compelling features including WYSIWYG Authoring, Photo Publishing and a Writer SDK. Now if only I could figure out how to make Writer communicate with my dynamic website engine.
I found that Live Writer supports the MetaWeblog API.
"The MetaWeblog API (MWA) is a programming interface that allows external programs to get and set the text and attributes of weblog posts. It builds on the popular XML-RPC communication protocol, with implementations available in many popular programming environments."
After only a few moments of "using the Google", I across a blog by George Trifonov that explained and provided example C# source of how to implement a blog engine that can communicate via the MetaWeblog API.
It was quickly obvious to me that it would take less code and make more sense to write a new dynamic website engine that reads the website content from the rss formatted file that George's code was already generating.
Using the Global_BeginRequest method in the Global Application Class, I wrote a quick function that inspects the incoming request file path and rewrites the path for server processing. This means that the incoming request of "Facilities.aspx" would be forwarded to page on the server called "ShowPage.aspx" with the url in the query string. i.e. (ShowPage.aspx?url=Facilities.aspx)
protected void Global_BeginRequest(object sender, EventArgs e)
{
string filename = Path.GetFileName(HttpContext.Current.Request.FilePath).ToLower();
switch(filename)
{
case "showpage.aspx":
case "webresource.axd":
case "metablogapi.ashx":
break;
default:
HttpContext.Current.RewritePath("showblog.aspx?url=" + filename);
break;
}
} |
You will notice that I listed ShowPage.aspx and other static files as pages that will be ignored by this function and allow ASP.NET to handle the request as it normally would.
The next step is to build the ShowPage.aspx page that will handle the request for the dynamic page, query the storage file and output the website content.
protected void Page_Load(object sender, EventArgs e)
{
string linkId = Server.UrlDecode(Request["url"]);
XmlDocument doc = new XmlDocument();
doc.Load(StorageFile);
XmlNode node = doc.SelectSingleNode("rss/channel/item[link ='" + linkId + "']");
if (node != null)
{
string blogContent = node.SelectSingleNode("description").InnerText;
ContentZone.Controls.AddAt(0, new LiteralControl(blogContent));
}
} |
The code in the Page_Load method firstly loads the RSS content into an XmlDocument object and performs a search for an item with a link that matches the incoming requested url. If found, we simply grab a reference to the description node as this is where the html content is stored. In my specific implementation for Miles Ahead Farm I've added support for output caching, html page title and also support for loading web user controls for needs such as a contact form.
You can download the Dynamic Website Engine Source Code from our downloads page.