A framework for creating HTTP services

Jonas Elfström / @jonelf



This presentation was created with reveal.js. You'll need a browser with support for CSS 3D transforms to see it in its full glory.

What's a Web API?

  • Uses HTTP standard (GET, POST, PUT, DELETE)
  • RESTful (REpresentational State Transfer)
    • Stateless
    • Cacheable
    • Client–server
    • A software architecture for distributed systems
  • Data format agnostic but usually JSON or XML.
  • Clean, semantic URLs,

Why a Web API instead of SOAP/WSDL?

  • Simpler
  • Based on the architecture of the web.
  • Easy to consume from JavaScript!
    • XMLHttpRequest
    • AJAX
    • jQuery $.get

$.get("user/4711", function(userData){
  alert("User: " + userData);

What's up with this REST thing?

That’s not really RESTful, it’s just kinda RESTful!

    • An acronym that makes babies cry
    • Hypermedia as the Engine of Application State
    • A REST client enters a REST application through a fixed URL
    • Future actions are discovered
    • Fielding says REST APIs must be hypertext-driven

Still not the norm

Richardson Maturity Model

  • Level 0 - Remote Procedure Invocation over HTTP
  • Level 1 - Resources, semantic URLs
  • Level 2 - HTTP Verbs
  • Level 3 - Hypermedia - HATEOAS

I need a definition!

  • WSDL 2.0 can describe REST services
  • WADL - Web Application Description Language
  • Almost never used...

I guess we'll just have to embrace the chaos and keep building snowflakes.

The history of .NET web frameworks


ASP.NET shipped with .NET in 2002.
Now known as ASP.NET Web Forms.

Focused on websites. Windows developers felt at home with the event-driven model.

Not good for creating a Web API. In that regard, nothing much changed between version 1.0, 1.1, and 2.0.

Supported SOAP and the [WebMethod] attribute was a simple way to turn a method into a XML Web service. Declared legacy technology by Microsoft.

ASP.NET 3.0 (2006)

Windows Communication Foundation
could use ASP.NET to host services.

Almost nobody understood how.

ASP.NET 3.5 (2007)

ASP.NET AJAX - an attempt to modernize ASP.NET.

Had JSON serialization and some even succeeded to use jQuery against it.

WCF still cumbersome to use.


Not even headbeards liked WCF

PS. WCF in .NET Framework 4 is easier to use but it's still a big framework. DS.

ASP.NET MVC 1.0 (2009)

In 2004 Ruby on Rails was released and made it big. It was followed by several other frameworks (Django, FubuMVC, OpenRasta, Castle MonoRail) based on the MVC pattern.

An alternative to ASP.NET Web Forms.

ASP.NET MVC is a better fit for HTTP services than ASP.NET Web Forms.


Do we really need MVC for HTTP services?

Could we do with something even simpler?

Even simpler frameworks

Sinatra, a lightweight Ruby framework, is often used to create RESTful APIs. It's a DSL for simple web applications.

require 'sinatra'

get '/hello/:name' do |name| 
  "Hello #{name}!"

Simple frameworks for C#

Nancy was inspired by Sinatra.

public class Module : NancyModule
    public Module()
        Get["/hello/{name}"] = x => {
            return string.Concat("Hello ",;

There's also Simple.Web, Manos, and more.

Sometimes these frameworks are a little bit too simple.


  • A sub-project type of ASP.NET MVC 4.
  • Content negotiation
  • Serializes to JSON, XML, or other.
  • Uses routing similar to ASP.NET MVC
  • Can be self-hosted.
  • Not instead of WCF.

Service Stack has similar properties.

Hanselman on ASP.NET Web API

There's Enterprise Web Services that use SOAP and WS-*.* and they are great for many transactional or complex scenarios. Then there are lighter weight RESTful web services or "Web APIs" that use JSON, XML and respect all of the goodness and stability that is the HTTP specification.
- Scott Hanselman

Move on

Create an ASP.NET Web API project

File -> New -> Project File->New Project

Create an ASP.NET Web API project


Web API project

ASP.NET Web API template

public class ValuesController : ApiController
	// GET api/values
	public IEnumerable Get()
		return new string[] { "value1", "value2" };

	// GET api/values/5
	public string Get(int id)
		return "value";

	// POST api/values
	public void Post(string value)

	// PUT api/values/5
	public void Put(int id, string value)

	// DELETE api/values/5
	public void Delete(int id)

ASP.NET Web API template

public class RouteConfig
  public static void RegisterRoutes(RouteCollection routes)

      name: "DefaultApi",
      routeTemplate: "api/{controller}/{id}",
      defaults: new { id = RouteParameter.Optional }

      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

API documentation

The default start page says nothing about your application, but that's easy to change.

Open the nuget console and run:

PM> Install-Package Microsoft.AspNet.WebApi.HelpPage -Pre

ASP.NET Web API Help Page

  • Uses the IApiExplorer
  • ApiExplorer ships with ASP.NET Web API
  • Can use autogenerated XML documentation

ASP.NET Web API: Introducing IApiExplorer/ApiExplorer

Intro video

Default start page


ASP.NET Web API Help Page


ASP.NET Web API Help Page


Enough already! How do I get it?

ASP.NET Web API is bundled with ASP.NET MVC 4.

For Visual Studio 2010 you can download it at

It's included in Visual Studio 2012.

Demo time!

Laptop fire


Jonas Elfström