6 msdn magazine
One of the things I like most in ASP.NET MVC is the ability to
expose a façade of methods that can be easily invoked from HTTP
clients, including jQuery-based pages, mobile apps and plain C#
back ends. For a long time, building this service layer took place
in the realm of Windows Communication Foundation (WCF)
services. Attempts were made to specialize WCF for HTTP, such
as the introduction of the webHttpBinding mechanism and frame-
works such as the now-retired REST Starter Kit. None of these
approaches, though, could really eliminate developer roadblocks
such as notorious WCF over-confi guration, overuse of attributes and
a structure not specifi cally designed for testability. Th en came Web
API—a new framework designed to be thin, testable, independent
from the hosting environment (for example, IIS) and HTTP-focused.
However, Web API has a programming interface that looks almost
too similar to ASP.NET MVC, in my opinion. Th is isn’t a negative
remark, though, as ASP.NET MVC has a clean and well-defi ned
programming interface. Web API actually started with a pro-
gramming model that looked similar to WCF and then grew to
resemble ASP.NET MVC.
In this article, I’ll provide a view of Web API from the per-
spective of the average ASP.NET MVC developer, and focus on
a functional area of Web API that represents a plus over plain
ASP.NET MVC: content negotiation.
Web API at a Glance
Web API is a framework you can use to create a library of classes that
can handle HTTP requests. Th e resulting library, along with some
initial confi guration settings, can be hosted in a runtime environment
and consumed by callers via HTTP. Public methods on controller
classes become HTTP endpoints. Confi gurable routing rules help
defi ne the form of URLs used to access specifi c methods. With the
exception of routing, however, most of what defi nes the default form
of URL handling in Web API is convention rather than confi guration.
If you’re an ASP.NET MVC developer, at this point you might
stop reading and wonder why on earth you’d want to use a new
framework that seems to just duplicate the concept of controllers
you “already have” in ASP.NET MVC.
Th e quick answer to that is, yes, you probably don’t need Web
API in ASP.NET MVC, because you can achieve nearly the same
functionality via plain controllers. For example, you can easily
return data formatted as JSON or XML strings. You can easily
return binary data or plain text. You can shape up the URL
templates you like best.
The same controller class can serve JSON data or an HTML
view, and you can easily separate controllers that return HTML
from controllers that just return data. In fact, a common practice
is to have an ApiController class in the project where you stuff all
endpoints expected to return plain data. Here’s an example:
public class ApiController : Controller
{
public ActionResult Customers()
{
var data = _repository.GetAllCustomers();
return Json(data, JsonRequestBehavior.AllowGet);
}
...
}
Web API uses the best of the ASP.NET MVC architecture and
improves it in two main areas. First, it introduces a new logical
layer known as content negotiation with a standard set of rules to
request data in a given format, whether JSON, XML or some other
format. Second, Web API has no dependencies whatsoever on
ASP.NET and IIS—more specifi cally, it has no dependency on the
system.web.dll library. Certainly it can be hosted in an ASP.NET
application under IIS. However, while this probably remains the
most common scenario, a Web API library can be hosted in any
other application that provides an ad hoc hosting environment,
such as a Windows service, a Windows Presentation Foundation
(WPF) application or a console application.
At the same time, if you’re an expert ASP.NET MVC developer,
the Web API concepts of controllers, model binding, routing and
action fi lters will be familiar to you.
Why Web Forms Developers Love Web API
If you’re an ASP.NET MVC developer, you might be initially
confused regarding the benefi ts of Web API because its program-
ming model looks nearly identical to ASP.NET MVC. However,
if you’re a Web Forms developer, you shouldn’t be confused. With
Web API, exposing HTTP endpoints from within a Web Forms
application is a child’s game. All it takes is adding one or more
classes similar to this:
public class ValuesController : ApiController
{
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
public string Get(int id)
{
return "value";
}
}
Content Negotiation and Web API for the
ASP.NET MVC Developer
CUTTING EDGE DINO ESPOSITO