Using WebAPI with ASP MVC 3

If you are like me where in your organization you are using the membership provider that came with ASP MVC 3 template but you also have the need to provide RESTful services from within the same application and would really really like to use the awesome WebAPI, here is what you need to do:

  • Create a brand new MVC 3 internet application or open up your existing application
  • Install the following NuGet Package using the package manager console:

    Install-Package AspNetWebApi

  • Open the Global.asax file and add the following route after this routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); line as shown:

    config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );

  • Also ensure that you have used or imported System.Web.Http depending on C# / VB in Global.asax like so:

    using System.Web.Http;

  • Add a new WebAPI controller using the Add -> New Item context menu option or just use Ctrl + Shift + A keyboard shortcut.

I believe this is a lot easier than starting a MVC4 application and trying to incorporate the membership provider into that.

Cheers!