WordPress Blog and ASP.NET MVC Integration

This was too easy/cool to keep tucked away. I wanted the most cheap and cheerful way of listing the top 10 blog posts, into a larger, containing website I am building for a client. I started out by Googling (googeloper) “wordpress integration c#”. Unexpectedly, nothing obvious I was looking for surfaced. Most solution seemed to rely on having WordPress installed physically beneath the website, or proposed options such as FTP based integration. Yuck! No. I just wanted the 10 latest posts in real time (i.e. as of now). Hold on I thought, that’s exactly what feeds have been designed to do. Blog engines (such as WordPress) have excellent feed support, including a REST based API, support for RSS and Atom. Wowzers.

There is plenty of good information about those topics elsewhere. I really just wanted to demonstrate how simple it is to query such a feed taking a server side approach (assuming your web server has Internet HTTP GET access to the feed). To do this, I used System.Net.WebClient, LINQ to XML and ASP.NET MVC 3. The feed I am consuming is for a WordPress blog, but there is absolutely no dependency/coupling that the blog must be WordPress based. It could for example, be a Blogger feed.

Another factor that should be given consideration is whether taking a server side (e.g. ASP.NET) approach versus a client side (e.g. jQuery) approach is a better fit for you. If for example search engine visibility (SEO) of the integrated blog content is important to you, a server side approach may be a better fit.

1. Using a browser review the feed you’re targeting. For example, if the (WordPress) blog is http://example.com then have a look at http://example.com/feed/ or http://example.com/feed/atom. Below is a sample of the first chunk of Atom feed for this WordPress blog.


	benCode
	Ben Simmonds: BizTalk Server Guy in Sydney

	2010-11-24T02:03:03Z

	
	http://bencode.net/feed/atom/
	

	WordPress.com


	
		
		
			beness
						http://www.bencode.net/
					
		<![CDATA[SSO Configuration Road Block]]>
		
		https://bencode.wordpress.com/2010/11/24/sso-configuration-road-block/
		2010-11-24T02:03:03Z
		2010-11-24T01:58:25Z
				]]>
		Recently I’ve had the need to setup a BizTalk Server 2006 R2 virtual machine. Quietly confident about my experience with this version of BizTalk, I jumped in head first to *quickly* get a simple single server based installation configured on a 32-bit VMWare based VM.

2. Below is a snippet of simple C# that leverages LINQ to XML to parse an Atom feed. This code is in a controllers action method, because of ASP.NET MVC, but again this code has no requirement for you to be using MVC. Side note: LINQ to XML makes working with XML much more fluid for the C#/VB developer. Very very nice to use!

public ActionResult Index()
{
  XNamespace xsd = "http://www.w3.org/2005/Atom";
  var client = new WebClient();
  var feed = client.DownloadString("http://www.bencode.net/feed/atom");
  var document = XDocument.Parse(feed);
  var blogs = 
    from e in document.Descendants(xsd + "entry")
    select new BlogModel()
    {
      Title = (string)e.Element(xsd + "title"),
      Content = new HtmlString((string)e.Element(xsd + "content"))
    };
  return View(blogs);
}

3. A simple MVC 3 Razor view:

@model IEnumerable

@{
    View.Title = "Blogs";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Blogs

@foreach (var blog in Model) {

@blog.Title

@blog.Content

}

4. Downloading, streaming, parsing and rendering this feed should be considered an expensive operation. Something that you probably don’t want to happen for each request that comes in for your page/site. To cache this entire “pipeline” of work, I went with some ASP.NET MVC output caching, by marking up the controller action with the following custom attribute:

[OutputCache(VaryByParam="none", Duration=60)]
public ActionResult Index()
{
  ...

The result:

software
Posted by: Ben Simmonds
Last revised: 28 Jun, 2011 01:17 PM

Comments

27 Oct, 2011 04:39 PM

hey Ben,

Thanks for putting this out. Great idea and one I may use.

Jaspreet

Greenbeaumn
Greenbeaumn
06 Feb, 2012 05:28 AM

PimaValmrit Anrielelin anadayagish http://napechke.com - Senanamaso Duetrydrype http://napechke.com

Your Comments

Used for your gravatar. Not required. Will not be public.
Posting code? Indent it by four spaces to make it look nice. Learn more about Markdown.

Preview