Web API is now part of ASP.NET (and you can get it today)
ASP.NET By Dave Ward. Updated February 17, 2012One of the hardest parts of being privy to NDA information is keeping my mouth shut about new developments that I wish I could share with you immediately, often for months at a time.
Recent developments around ASP.NET Web API (formerly WCF Web API) are a perfect example of that conundrum. As development on WCF Web API seemingly stagnated on CodePlex, Microsoft had actually rolled the project into ASP.NET itself. The project was in no danger whatsoever, but (frustratingly) I wasn’t able to tell you that.
So, it’s a relief that today’s today’s ASP.NET MVC 4 Beta release has made that news public:
Top Features
- ASP.NET Web API
- Refreshed and modernized default project templates
- New mobile project template
- Many new features to support mobile apps
- Recipes to customize code generation
- Enhanced support for asynchronous methods
Though Web API isn’t necessarily a gigantic step forward from using MVC’s controllers as a makeshift API for simple scenarios, it’s great that ASP.NET now has common mechanism for creating these endpoints that works the same way on both WebForms and MVC.
I’ve long held out against pressure to move from ASMX to WCF in WebForms projects, because accepting WCF’s complexity primarily only rewarded me with less flexible JSON serialization. By contrast, I’ve begun converting some of my projects from ASMX to Web API, and have been pleased with how easily Web API replaces ASMX.
I believe Microsoft has finally found a good balance between ASMX’s simplicity and WCF’s power with Web API.
Since it was built with jQuery in mind from the very start, I’m finding that Web API is perfect for the sort of work that I usually write about here. It even automatically responses to requests from jQuery with JSON, even if you use simple URL encoded parameters with the request, so adding an endpoint for AJAX interaction in either WebForms or MVC is a breeze now.
I’ll be writing more about actually using Web API here in the future, but I wanted to get this post published right away to help spread the news that Web API isn’t dead.
To learn more about what exactly Web API is and does, have a look at its section in the release notes. To get up and running quickly with some example code, take a look at the new Web API material on the ASP.NET site.
Similar posts
What do you think?
I appreciate all of your comments, but please try to stay on topic. If you have a question unrelated to this post, I recommend posting on the ASP.NET forums or Stack Overflow instead.
If you're replying to another comment, use the threading feature by clicking "Reply to this comment" before submitting your own.



Awesome! I think we have a project in mind (…or two) that might be a good utilization of this…
Thanks for the heads up!
> Note: It’s not entirely clear, but installing the MVC 4 beta allows you to use
> Web API in WebForms projects too.
However, you can only select Razor as your view engine when using MVC. No ASPX.
Exciting stuff! Our ASMX’s were due to be replaced by WCF within the next 6 months anyway.
I hadn’t given it much thought but maybe this will be a more attractive alternative! Does anyone have any idea of release dates for MVC 4 since we’re only in Beta now? I know MVC has previously shipped quite often but had heard it was due to be slower post MVC 3….
I don’t know if this helps, but it looks like the Web API portion of this (which shouldn’t ultimately be tied to MVC at all) is also on NuGet now: http://www.nuget.org/packages/AspNetWebApi
I’d quite like the DataControllerMetadataGenerator used in the Single Page Application versions of Web API to be plugable, to allow custom metadata to be sent to the client.
Good stuff Dave. It’s frustrating that we now have yet another option to consider when doing AJAX calls, but this does sound like a great compromise. FYI – “pleased with easily Web API replaces ASMX” <-I believe you're missing the word "how".
Yep. Thanks for pointing that out.
AJAX calls are always a worry to us – we would love unique and easy way to go. And yes uniform way on both asp.net forms and MVC would be appreciated. If web API can be a turn towards it, it will be great. Waiting patiently hands-on tutorials on it. Thanks Dave!
Looking forward to know more from your Next Post , Thanks
I don’t like having to have my services linked into MVC application. Also I think that not having the UriTemplate is a massive oversight. This, to me, is a step backwards from WCF Web API (which was awesome).
It looks as though ASP.NET Web API will be great for VERY basic APIs but lacking for anything more.
There’s no requirement that Web APIs live inside an MVC project. You can self-host them and host them within WebForms projects too. This MVC 4 Beta release is just the first time the new incarnation has been available.
Is there anything you can do with UriTemplate that you can’t do with MapHttpRoute, or do you just prefer having that next to the method?
You may be able to do it with mapping routes, but IMHO it is tidier and easier to understand using the UriTemplate. You could end up with a good number of mappings, and they are further removed from what they are relevant to.
On a slight side-note, is it still possible to access the HttpRequestMessage and HttpResponseMessage?
One of the things that I liked most in WCF Web API is that you could explicitly map a route to a service (type) using RouteTable.Routes.MapServiceRoute rather than using magic to find a controller from who-knows-where? I know that you can add a namespace to a mapping. But that is a little too loosely coupled for my comfort. Does this (or something similar) still exist, or is it going to be brought back?
Hi Dave,
Since I read this post I’ve been reading about the web api and it seems interesting (this is my first foray into anything more than old style asmx). I can understand the basics of a GET and a GET with an ID, e.g. GetContacts, GetContact(int ID). What I’m really struggling with is the idea of searching using web api. Is it possible? Is it supposed to be used in that way? For instance, if I want to pass say 5 parameters to the GET how would I do that? I imagine in jquery I just add the parameters as normal but how do I represent it in the controller and the routing table in my Global.asax file?
This is my basic route, I simply can’t find any resources to explain how to add additional parameters.
RouteTable.Routes.MapHttpRoute(“DefaultApi”, “api/{controller}/{id}”);
Unless, each parameter has to come after the api? e.g.
RouteTable.Routes.MapHttpRoute(“DefaultApi”, “api/{controller}/{id}/{name}/{email}”);
That seems a bit strange to me though.
Anyway, any advice would be greatly appreciated.
Thanks
Jon
You can pass parameters on the QueryString without specifically defining them in the route. So, you could have a route like this:
And a controller like this:
Which you could GET like this:
Or, if you’re using jQuery:
Hi Dave,
Thanks for this comment on passing parameters to a controller. I wonder if you might consider expanding on this in a future blog post?
I (probably like many others) found I could do the same myself but only through trial and error and general fiddling. I think that your comment is the only documented examples of doing this I’ve seen.
Just a thought. Thanks for the good work!
John
Hi Dave,
Thanks for this. Very useful and quite simple when you see an actual example.
Thanks very much.
Jon