ASP.NET Web API 2 is out! Overview of features

Β· 809 words Β· 4 minutes to read

Today the ASP.NET team released ASP.NET Web API 2 (and, for that matter, MVC 5 too)! The announcement, just as like year, followed many other big Microsoft releases (Windows 8.1, Visual Studio 2013 etc) in a synchronized product shipping event.

New Web API is now available on Nuget - where it directly replaces the old version of Web API. Let’s look at the major features of ASP.NET Web API 2.

Quick overview πŸ”—

Web API is open sourced, and v3-RTM is tagged to include all the changes until September 17 (plus a few later tiny commits). The major new features are listed below.

1. AttributeRouting πŸ”—

By far my favorite Web API changeset; brings in the excellent Attribute Routing library by Tim McCall into the core of the framework, solving many of the routing issues.

With attribute routing, we can now have both HTTP VERB and action based routing on the same controller. We can achieve RESTful hierarchy (nested routing) on a single controller without any problems. Furthermore, we can also have different versions of the API routing to the same controller.

Remember, from today:

public static class WebApiConfig  
{  
public static void Register(HttpConfiguration config)  
{  
config.MapHttpAttributeRoutes();  
}  
}  

should become your favorite line in the Web API applications!

Full specification for AR is available here.

Additionally, Mike Wasson has written a terrific introductory article, which I really recommend.

2. OWIN self host πŸ”—

Web API 2 introduces a new self host package, called Microsoft.AspNet.WebApi.OwinSelfHost. It allows you to host Web API using Katana. There are plenty of examples of self hosting Web API on top of OWIN, but since Katana assemblies have been evolving very rapidly, most of them (including the few ones found on this blog) tend to be out of date as the APIs changed.

The quickest and smallest code to get you up and runnings is:

public class Startup  
{  
public void Configuration(IAppBuilder appBuilder)  
{  
var config = new HttpConfiguration();  
config.Routes.MapHttpRoute(  
name: "DefaultApi",  
routeTemplate: "api/{controller}/{id}",  
defaults: new { id = RouteParameter.Optional }  
); 

appBuilder.UseWebApi(config);  
}  
}

public class Program  
{  
static void Main()  
{  
using (WebApp.Start<Startup>("http://localhost:999/"))  
{  
Console.ReadLine();  
}  
}  
}  

3. IHttpActionResult πŸ”—

A new way of returning HttpResponseMessage from your controllers - which improves code reusability, and testability. I have blogged about this feature in June after it first popped up in the nightly builds, so make sure to check out the post to learn more.

4. CORS πŸ”—

Another terrific feature contributed by the community, more specifically, Brock Allen. It brings a very comprehensive, yet intuitive to use, implementation of the Cross Origin Resource Sharing specification to Web API developers.

Feature specification is also available at the codeplex site.

Just like in the Attribute Routing example, Mike Wasson has a nice article that will quickly get up and running.

5. HttpRequestContext πŸ”—

A more elegant way of dealing with per-request contextual information. Similarily to IHttpActionResult, a while ago, I dedicated a separate post to examine this feature, so you may want to have a look at it. The additional role of HttpRequestContext is to improve testability and reduce the amount of set up code needed to get tests up and running.

6. Testability πŸ”—

This brings us to the next point and that is the testability of Web API. Compared to the old version, where testing code required lots of boostraping in the controller or resorting to helper libraries like WebAPiContrib, the set up code needed to test controllers in Web API 2 is much smaller and less noisy.

The ASP.NET team has provided the summary for these improvements here

7. OData improvements πŸ”—

Web API 2 adds support for $expand, $select, and $value.

    • $expand behaves like Include in Entity Framework, as it forces a load of a related entity (or entities).
    • $select allows the client to ask for specific properties of the entity only
    • $value allows the client to get raw value of a given property

The feature specification with more details can be found here.

8. Filter Overrides πŸ”—

Small but very useful feature allowing you to override widely scoped filters (i.e. global level or controller level), from a narrower scope (i.e. action level). I have blogged about this feature, along with some examples, back in June.

9. ByteRangeStreamContent πŸ”—

This is a bit of stretch, as it was really released as part of the ASP.NET Web API WebTools 2012.2 Update in May 2013, but it’s worth mentioning here, as it kind of got lost in translation. Introduction of this new type of HttpContent allows the client to easily request partial entities (HTTP status code 206) using the Content-Range HTTP header.

The original blog post from Henrik Nielsen, including code samples can be found here.

Summary πŸ”—

I believe these are all of the main features! If I missed something let me know, in the meantime - head off to Nuget and enjoy the new ASP.NET Web API 2!

Install-Package Microsoft.AspNet.WebApi  

About


Hi! I'm Filip W., a cloud architect from ZΓΌrich πŸ‡¨πŸ‡­. I like Toronto Maple Leafs πŸ‡¨πŸ‡¦, Rancid and quantum computing. Oh, and I love the Lowlands 🏴󠁧󠁒󠁳󠁣󠁴󠁿.

You can find me on Github and on Mastodon.

My Introduction to Quantum Computing with Q# and QDK book
Microsoft MVP