API Versioning in ASP.NET Web API 2.1

Thanks to new changes to Attribute Routing in ASP.NET Web API 2.1 there are two easy ways to version your RESTful API now. Let’s take a look at two approaches we can use for API versioning in Web API 2.1:

Approach 1
  1. Separate your controllers of different versions by file naming (e.g. MyV1Controller, MyV2Controller) or by namespaces (e.g. MyApi.V1.MyController and MyApi.V2.MyController)
  2. Use VersionedRoute atrribue for your controller:
    [VersionedRoute("api/my", 1)]
    public class MyV1Controller : ApiController { ... }
    [VersionedRoute("api/my", 2)]
    public class MyV2Controller : ApiController { ... }

    Now client should include the following header item in the request:
    api-version: 2

Approach 2

Another approach to have version number in the URI is to use RoutePrefix instead as follows:

public class MyV1Controller : ApiController { ... }
public class MyV2Controller : ApiController { ... }

It’s a little bit easier because there is no need to set API Version header but considered as a second class solution when talking about best practices ;-)

Dig more!


Automate Your Social Content Publishing

There are many people in the world who use powerful social media tools like Twitter, Facebook, etc to communicate with  their friends, find interesting contents, to be the first to get notified of the breaking news and more. Many of us who are active in any field are using these tools to produce and publish contents as well looking at the fun side of these tools.

Personally I’m accustomed to share interesting links, apps, quotes or any other thing I find during my daily web surfing and let my friends and followers enjoy them too. You may have many feeds which you take a look every morning and select some of them to read and maybe share; but it’s not a good idea to share them all at once and flood your friends timeline with consecutive tweets or status updates! This will also decrease the chance for them to be read.

Said all these to introduce some services which can help you schedule such updates in an easy and productive way; let’s start with the problem:

I want to tweet every single item which I star in my Google Reader but not at once, schedule them to publish during the upcoming hours.

My solution is to use various services to accomplish this:

  • I will use IFTTT to automate publishing contents which I star in Google Reader.
  • I will use Buffer to schedule tweet publishing throughout the day.
  • I will use Tweriod to find out which hours are the best to schedule my tweets to send out.

Now let me explain more; First of all go to bufferapp.com and signup; it lets you send and update from their website or schedule it based on the hours you specify in the Settings page. You can also authorize this app with your bit.ly account so that it posts links with your own custom URL shortenner (like mine: go.mahdi.ws); then go to ifttt.com and signup; IFTTT lets you connect various service together and run them as automated scheduled tasks. In our case you need to select Buffer and Google Reader from Channels and add them to your channels; after that you can create your own recipe to post starred item in Google Reader to Buffer or use my ready recipe for this.

Congratulations! You’re done! Starting from now, all your starred items in Google reader will be automatically posted to your Buffer queue and will be published during the day.

There is one more cool optional step to use if you like ;-) Tweriod gives you a report on what are the best hours to send out your tweets based on various parameters; sign up and wait for your first report to arrive in your mailbox; then you can adjust the best posting hours for your tweets just by connecting your Buffer account to Tweroid and let it do its job!