This evening, Microsoft has released a Fall 2012 Update to ASP.NET. The release brings a whole set of goodies for ASP.NET developers, including SignalR (now officially part of ASP.NET), C# FB app project template, Windows Azure Authentication and more. Jon Galloway has the updates very nicely summarized here.
Of course, as you’d expect, there are plenty of updates regarding Web API. Let’s have a look at them.
More after the jump.
What’s new 🔗
There are a few new (old) things for Web API developers in the update.
Firstly, it’s important to note that nowadays ASP.NET-related releases aren’t that big of a deal any more - obviously because all the stuff that’s related to MVC and Web API is publicly available on Codeplex and in the Nuget nightly build packages (and - by the way - that’s just great!). So really, you can always just follow these channels and stay up to date with the latest developments in the area that interests you (like Web API).
But if you don’t… this update brings you all the following changes into your Visual Studio 2012 templates:
- Single Page Application MVC4 template
- Web API OData support is now part of MVC4 Web API template
- Web API Tracing is now part of MVC4 Web API template
- Web API Help Page is now part of MVC4 Web API template
Note, the latter three are upgraded from their 0.1 versions to 0.2 version. The biggest change is that they are part of any new Web API projects by default, and you no longer have to manually pull them off NuGet.
OData support 🔗
Till now, if you were working with OData in Web API, you were almost certainly using the preview package whuch has been available through Nuget since mid-August. The new release ups the version from 0.1 to 0.2 (mind you - it’s still alpha!), and can also be grabbed from Nuget if needed.
Once you create a new default Web API MVC4 template, you’d notice a couple of DLL references that are relevant for OData support:
- the new System.Web.Http.OData dll
- Microsoft.Data.OData, which is the base for the functionality
- Microsoft.Data.Edm
- Microsoft.Data.OData.Contrib providing community driven extensions
The switch from 0.1 to 0.2 provides a considerable set of updates:
- adds an OData formatter that supports both the Atom and JSON media type formats
- supports Entity Type Inheritance (more info here)
- support for [Queryable] result limit
- support for enums in queries
- adds support for OData actions - an overview can be found here
- allows string comparisons (lt, le, gt, ge) in OData queries
- lots of other improvements to functionalities that were already available in the initial release, such as Queryable attribute, $metadata support, nice support for abstract syntax tree representation, relationships between entities and many more
Query support is “switched on” by a single line in the WebAPiConfig class, which appears by default:
config.EnableQuerySupport();
By the way, a while ago I blogged about supporting $inlinecount with the OData package.
Web API help page 🔗
Web API help page has also been available on Nuget since August as 0.1 version. Like the OData library, it has also been upgraded to v0.2 .
It is an extremely helpful bit of code, allowing you to very easily create documentation for your API without having to work with low level abstractions such as IApiExplorer and manually stitching together a snapshot of your API. Moreover, it will automatically pull your method comments into the Help page, which is super convenient.
There aren’t a lot of changes between 0.1 and 0.2 versions, but the UI is now cleaned up and no longer depends on JQuery UI. Yao Huang from MSFT has a great overview post of the latest 0.2 version.
Web API help page is configurable via its own separate area registration class, HelpPageAreaRegistration.
public class HelpPageAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "HelpPage";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"HelpPage_Default",
"Help/{action}/{apiId}",
new { controller = "Help", action = "Index", apiId = UrlParameter.Optional });
HelpPageConfig.Register(GlobalConfiguration.Configuration);
}
}
After you run the MVC4 application, the help page link is available in the top right hand corner of the template.
Web API Tracing 🔗
Finally, the Web API tracing package also got upgraded from 0.1 to 0.2 version.
The package contains a wrapper around ITraceWriter which allows you to trace the internals of Web API directly to System.Diagnostics.Trace throgh an adapter class System.Web.Http.Tracing.SystemDiagnosticsTraceWriter . Everything is configurable inside TraceConfig udner App_Start.
public static class TraceConfig
{
public static void Register(HttpConfiguration configuration)
{
if (configuration == null)
{
throw new ArgumentNullException("configuration");
}
SystemDiagnosticsTraceWriter traceWriter =
new SystemDiagnosticsTraceWriter()
{
MinimumLevel = TraceLevel.Info,
IsVerbose = false
};
configuration.Services.Replace(typeof(ITraceWriter), traceWriter);
}
}
With the update, tracing is enabled by default - so you no longer have to search Nuget for the package or uncomment stuff (as in previous release). Please note that this is only one of the possible ITraceWriter implementations - a while ago I have blogged about an alternative, using NLog instead of Trace - you can read more about that in this post.
Single Page Application template 🔗
It is a replacement template for an old (shipped with VS 2012 beta) SPA template built on top of an invention called DataControllers and a custom JavaScript library Upshot.js. The new one is much slicker, especially as it doesn’t try to re-invent the wheel and simply uses Web API and Knockout.js to provide you with a scaffold for a Single Page App.
The sample application is a - of course - to do list.
Summary 🔗
As we have gotten used to recently, Microsoft once again does a really nice job in putting a comprehensive set of preview features out into a public. And since pretty much all of this stuff (at least the Web API related) is open sourced, it allows many developers and early adopters to have a chance to get all the features in a nice, single package, effectively a click away.
The new Web API bits - especially the OData stuff - are definitely very promising. So grab the vNext preview installer and have a look yourself!
Finally, tomorrow (or today, depends on how you look at it), don’t forget about a terrific BUILD session by MSFT’s Daniel Roth - Building Services for Any Client with ASP.NET Web API at 2:30pm PST, 5:30pm EST.