In case you missed the news, the beta of ASP.NET MVC has been released. You can download these latest bits MVC 4 Beta bits here using the Web Platform Installer or you can download the complete stand alone setup package instead. This installs side-by-side with ASP.NET MVC 3 so you can started working with the latest in 4 without impacted your existing MVC3 projects. Also important to note, this release includes a “Go-Live” license so you truly can start using this release. (Note, that all standard caveats apply, test your stuff, things may change before RTW)
The top features listed in this new release are:
- 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
And much, much more!
One of the new and exciting additions mentioned in the list above is the ASP.NET Web API. Originally know as the WCF Web API (Codeplex Link) it has now be integrated tighter with ASP.NET. And why do you care? In this day and age, almost anything worth connecting to on the web has some sort of API, facilitating communication from a wide range of clients from browsers, to phones and tablets. The ASP.NET Web API provides a “modern HTTP programming model.”
It’s easy to get started by selecting the “Web API” template after selecting an MVC 4 Project type.

The new project will contain a sample ValuesController class that shows how to get things rolling.
public class ValuesController : ApiController
{
// GET /api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET /api/values/5
public string Get(int id)
{
return "value";
}
// 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)
{
}
}
Now if you’re familiar with both MVC and common Web API’s, the next step should come as no surprise. Making a call to http://localhost:33214/api/values will call the ValuesController, and execute the Get method. In a browser the result may at first appear like this:

Here’s where you may be confused if you’re not familiar with web API’s. To understand what’s going on I would encourage you to take a look at the browser’s request and associated response returned using a tool such as the F12 Developer tools in IE9, Fiddler, or Firebug or other developer tools in other browsers.
By enabling the F12 Developer tools, selecting the “Network” tab and clicking on Start Capturing, you will be able to see details for the http traffic.

The key think to note is the Type, or mime type associated with the response. You will see that it is “application/json” instead of the normal “text/html” that an HTML page would normally return. If I go ahead and tell IE to open the file and look at it in notepad we will see the json representation returned by the controller.

This result corresponds directly to the controller code:
// GET /api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
Simple, and extremely powerful. I’ll be posting more on the new ASP.NET Web API and other ASP.NET MVC4 features in the days ahead.
Posted in |