I am currently learning how to implement a RESTful API and am using RESTful API Tutorial as a resource.  I quickly needed to add functionality to my existing ASP.NET MVC site that I am building hence the need to document my steps for others who might have a need in doing this also.

I am working in Visual Studio 2013 and ASP.NET MVC 5.

Add a New Controller

This is fairly easy to do.  Right click on the Controllers folder.  Select “Add > Controller”.  The “Add Scaffold” dialog will appear.

Add Scaffold

At this point, you can choose the Web API you would like to add.  I chose “Web API 2 Controller with read/write actions” to get some samples to start working with.

Visual Studio will then add the new controller, references to System.Web.Http  and System.Web.Routing  and open a new readme.txt document with additional instructions.  I will be documenting these instructions below.

Add Using Statements

At the beginning of the Global.asax.cs file, add the following:

using System.Web.Http;
using System.Web.Routing;

 Add Application_Start in the Global.asax

If you don’t already have it, add Application_Start() to your Global.asax.cs file.

protected void Application_Start()
{

}

Add GlobalConfiguration.Configure(WebApiConfig.Register) to the beginning of Application_Start().  Here is what mine looks like now:

protected void Application_Start()
{
     GlobalConfiguration.Configure(WebApiConfig.Register);
     AreaRegistration.RegisterAllAreas();
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
     RouteConfig.RegisterRoutes(RouteTable.Routes);
     BundleConfig.RegisterBundles(BundleTable.Bundles);
     BundleMobileConfig.RegisterBundles(BundleTable.Bundles);
}

And that is all you need to do.  You can use RESTful Api Tutorial to learn more about RESTful API’s.

Leave a Reply

Your email address will not be published. Required fields are marked *