msdnmagazine.com
magazine
MSDN
Magazine
Online
It’s like MSDN Magazine—
only better. In addition to all the
great articles from the print
edition, you get:
- Code Downloads
- The MSDN Magazine Blog
- Digital Magazine Downloads
- Searchable Content
All of this and more at
msdn.microsoft.com/magazine
code shows how to set the Accept header in a jQuery call to a Web
API endpoint to get back some XML:
$.ajax({
url: "/api/news/all",
type: "GET",
headers: { Accept: "text/xml; charset=utf-8" }
});
In C# code, you set the Accept header like this:
var client = new HttpClient();
client.Headers.Add("Accept", "text/xml; charset=utf-8");
Any HTTP API in any programming environment lets you set
HTTP headers. And if you foresee that you can have callers where
this might be an issue, a best practice is to also add a media type
mapping so the URL contains all the required information about
content formatting.
Bear in mind that the response strictly depends on the structure
of the HTTP request. Try requesting a Web API URL from the
address bar of Internet Explorer 10 and Chrome. Don’t be surprised
to see you get JSON in one case and XML in the other. Th e default
Accept headers might be diff erent in various browsers. In general,
if the API will be publicly used by third parties, you should have a
URL-based mechanism to select the output format.
Scenarios for Using Web API
Architecturally speaking, Web API is a big step forward. It’s
becoming even more important with the recent Open Web Interface
for .NET (OWIN) NuGet package (Microsoft.AspNet.Web -
Api.Owin) and Project Katana, which facilitate hosting the API
in external apps through a standard set of interfaces. If you’re
building solutions other than ASP.NET MVC applications, using
Web API is a no-brainer. But what’s the point of using Web API
within a Web solution based on ASP.NET MVC?
With plain ASP.NET MVC, you can easily build an HTTP façade
without learning new things. You can negotiate content fairly easily
with just a bit of code in some controller base class or in any method
that needs it (or by creating a negotiated ActionResult). It’s as easy as
having an extra parameter in the action method signature, checking
it and then serializing the response to XML or JSON accordingly.
Th is solution is practical as long as you limit yourself to using XML
or JSON. But if you have more formats to take into account, you’ll
probably want to use Web API.
As previously mentioned, Web API can be hosted outside of IIS—
for example, in a Windows service. Clearly, if the API lives within an
ASP.NET MVC application, you’re bound to IIS. Th e type of hosting
therefore depends on the goals of the API layer you’re creating. If it’s
meant to be consumed only by the surrounding ASP.NET MVC site,
then you probably don’t need Web API. If your created API layer
is really a “service” for exposing the API of some business context,
then Web API used within ASP.NET MVC makes good sense. Q
DINO ESPOSITO is the author of “Architecting Mobile Solutions for the Enterprise”
(Microsoft Press, 2012) and the upcoming “Programming ASP.NET MVC 5”
(Microsoft Press). A technical evangelist for the .NET and Android platforms at
JetBrains and frequent speaker at industry events worldwide, Esposito shares his vision
of soft ware at soft ware2cents.wordpress.com and on Twitter at twitter.com/despos.
THANKS to the following technical expert for reviewing this article:
Howard Dierking (Microsoft )