I’ve been looking at one of  the new  features of ASP.NET Web API and I decided to blog about it. I’ve used a Visual Studio 2010 with MVC 4 Tempalte for the blog.  Here is my experience on the matter.

It’s been said that the Web API  allows you to create services that can be exposed over HTTP rather than through a formal service such as WCF or SOAP.  When request comes a controller is picked up based on HTTP verb specified rather than on url.  Having communication over HTTP avoids complications with Firewalls and security issues.

The ASP.NET Web API includes support for the following features:

  • Modern HTTP programming model: Directly access and manipulate HTTP requests and responses in your Web APIs using a new, strongly typed HTTP object model.
  • Full support for routes: Web APIs now support the full set of route capabilities that have always been a part of the Web stack, including route parameters and constraints.
  • Content negotiation: The client and server can work together to determine the right format for data being returned from an API. There is provided support for XML, JSON, and Form URL-encoded formats.
  • Model binding and validation: Model binders provide an easy way to extract data from various parts of an HTTP request and convert those message parts into .NET objects which can be used by the Web API actions.
  • Filters: Web API  supports filters, including well-known filters such as the [Authorize] attribute.
  • Query composition: By simply returning IQueryable<T>, the  Web API will support querying via the OData URL conventions.
  • Improved testability of HTTP details: Rather than setting HTTP details in static context objects, Web API actions can now work with instances of HttpRequestMessage and HttpResponseMessage. Generic versions of these objects also exist to let you work with your custom types in addition to the HTTP types.
  • Improved Inversion of Control (IoC) via DependencyResolver: Web API now uses the service locator pattern implemented by MVC’s dependency resolver to obtain instances for many different facilities.
  • Code-based configuration: Web API configuration is accomplished solely through code, leaving your config files clean.
  • Self-host: Web APIs can be hosted in your own process in addition to IIS while still using the full power of routes and other features of Web API.

At the very beginning of this journey I run Visual Studio(VS)  then  chose from the VS menu “Create a New project” => “ASP.NET MVC 4 Web Application” after that I ended up  with the following solution structure:


As can be seen it’s a pretty familiar ASP.NET MVC  app. I will list the structure of both controllers created by default,   HomeController and ValueController


public class HomeController : Controller
      public ActionResult Index()
          return View();


public class ValuesController : ApiController
   // GET /api/values
   public IEnumerable&lt;string&gt; Get()
      return new string[] { &quot;value1&quot;, &quot;value2&quot; };
   // GET /api/values/5
   public string Get(int id)
      return &quot;value&quot;;
   // 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)

An important point to notice  here is how the routes are defined. A quick look at the Global.cs file gives us the answer:

public static void RegisterRoutes(RouteCollection routes)

       name: &quot;DefaultApi&quot;,
       routeTemplate: &quot;api/{controller}/{id}&quot;,
       defaults: new { id = RouteParameter.Optional });

       name: &quot;Default&quot;,
       url: &quot;{controller}/{action}/{id}&quot;,
       defaults: new { controller = &quot;Home&quot;,
           action = &quot;Index&quot;, id = UrlParameter.Optional });

Going forward I’ve made a test with Fiddler tool as I called the Get() method from our ValueController:

Fiddler - initiate request

Here is the response :

HTTP Response details

In the “Http Response details” screenshot is seen that the returned result by the Get() method is in form of JSON.  You can try to call the other methods as well through the Fiddler tool.

For additional information on the matter you can check out the following  links:


Hope have fun!




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s