Building a Twitter Retweet Engine w/ ASP.NET MVC (Part 2)

Thursday, November 18, 2010

Solution Setup

For this project, I went with my tried and true solution structure:


I copied the System.Web.Mvc assembly to the lib folder and updated the reference accordingly.  I always recommend this approach especially when dealing with pre-release libraries.  The New Project dialog adds a reference to the default installation directory for the ASP.NET MVC version you choose.  This is usually “C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 3\Assemblies”.  The problem with this is that your web host probably doesn’t install pre-release software.  By including it as a local library and setting “Copy Local” to true you can ensure that the required version is copied to the output directory.  It also helps when you add to version control and want to update the library to the latest release candidate.
copy local true

I also did the same for the LinqToTwitter and DotNetOpenAuth libraries.

Razor Template

Before you start using Razor templates, I highly recommend you read Scott Guthrie’s overview on Razor layouts.   There’s a couple new files you need to be familiar with including  _ViewStart.cshtml and _Layout.cshtml.  Essentially, _ViewStart removes the need for you to explicitly set the layout for each view you add and specify any common view code you want to have execute before rendering the view.  _Layout is what you commonly know as your Site.Master page.

First Try – Twitter API

To wrap this post up, I want to just take a quick look at wiring up all the Twitter API authentication process. The first step to get OAuth working is to get a consumer key and secret from Twitter. It’s a quick and easy process. Just go to and select “Register a New Application”. Complete all the required fields and you’ll be presented with the key and secret. Make sure you select “Browser” as the Application Type. Don’t worry about the callback URL for now – just put or something similar for now.

Next, add the required assemblies that you downloaded with Linq to Twitter to your project. These include LinqToTwitter.dll and DotNetOpenAuth.dll. An important step is to create a class that implements the IConsumerTokenManager interface in the DotNetOpenAuth library. This is the class that will manage all the access tokens and secrets for OAuth. For the sake of this quick POC, I’m using the InMemoryTokenManager included in the Using OAuth with ASP.NET MVC sample project. The only configuration change you have to make is to add a your consumer secret and key to the Web.config as illustrated below:

Linq to Twitter Configuration Settings

Linq to Twitter Configuration Settings

Take a look at that project to get a quick connection to the Twitter API going. I ran my project and was able to connect exactly as expected. Next time we’ll look at the wireframes for the application and getting more of the functionality of the site going.

Leave a Reply