3 Simple Steps to Create Your First ASP.Net Web API Service

The ASP.NET Web API is a framework that simplifies the creation of HTTP services. Using the ASP.NET Web API we can create HTTP services that are not SOAP-based, like plain XML or JSON strings and so on and with additional advantages.

  • Allowing creation of resource-oriented services using the full features of HTTP.
  • Exposing services to a variety of clients easily like browsers or mobile devices and so on.

Before diving into details, I would like to clarify one misconception that the ASP.NET Web API has replaced WCF. WCF is still a powerful programming model for creating SOAP-based services that use a variety of transport protocols like HTTP, TCP, Named Pipes or MSMQ and so on. You can find the same implementation by using WCF REST in another article, "5 simple steps to create your first RESTful service".

Apart from Visual Studio 2010 or 2012, we also need MVC 4.0 to implement this HTTP service. For the purpose of this implementation, I will use Visual Studio 2010.

You can download MVC 4.0 for Visual Studio 2010 from Microsoft as follows:

The following are the 3 simple steps to create a HTTP service that returns non-SOAP based data.

  • Create Web API Project
  • Prepare domain Model
  • Adding Controller class

Let's move forward to the procedure to create a simple HTTP service using the ASP.NET Web API.

  1. Create Web API Project
    • Open Visual Studio and create a "New Project", in other words "File" -> "New" -> "Project...".
    • Choose the "ASP.NET MVC 4 Web Application" template and name the project as "FirstWebAPIService".
    • When you click the "OK" button, a new window will appear for selecting a sub-template. Actually for ASP.NET MVC 4 Web Applications, we have multiple sub-options, in other words Empty, Internet Application, Web API and so on.
    • Choose "Web API" and simply press the "OK" button.

      1.0-WebAPI.jpg

      1.1-WebAPI.jpg

    • A default ASP.NET MVC 4 Web API template project is created. Since it's an MVC application template, you will easily find "Model", "View" and "Controller" folders inside it.
       
  2. Preparing domain Model

    Now in the second step, we need to prepare the model.
     
    • Right-click on the "Model" folder and choose "Class" under "Add" from the context menu as shown in the figure.
    • Name the class "Product.cs".

      2.0-WebAPI.jpg

      2.1-WebAPI.jpg

    Here is the code for the Product class.
    1. public class Product  
    2. {  
    3.     public int ProductID { getset; }  
    4.     public string ProductName { getset; }  
    5.     public string ProductCategory { getset; }  
    6.     public int Price { getset; }  
    7. }
  3. Adding Controller Class

    The Controller class plays an important role, because requests coming from the client hits the controller first. Then the controller decides which model to use to serve the incoming request. So, in order to add a controller:
    • Right-click on the "Controller" folder and choose "Controller" under "Add" from the context menu as shown in the figure.
    • Name the controller "ProductsController".

      3.0-WebAPI.jpg

      3.1-WebAPI.jpg

    • Click the "Add" button, a new controller class will be added.

      In order to make the sample simple, we will load the model with data inside this controller instead of loading it from a database. The following is the code for the controller class.
      1. public class Products Controller : ApiController  
      2. {  
      3.     Product[] products = newProduct[]  
      4.     {  
      5.         new Product { ProductID = 1, ProductName = "Product 1", ProductCategory= "Category 1", Price = 120 },  
      6.         new Product { ProductID = 2, ProductName = "Product 2", ProductCategory= "Category 1", Price = 100 },  
      7.         new Product { ProductID = 3, ProductName = "Product 3", ProductCategory= "Category 2", Price = 150 },  
      8.         new Product { ProductID = 4, ProductName = "Product 4", ProductCategory= "Category 3", Price = 90 }  
      9.     };  
      10.     publicI Enumerable<Product> GetProducts()  
      11.     {  
      12.         return products;  
      13.     }  

Don't forget to add "using FirstWebAPIService.Models;" at the top of the controller class.

Now, it's time to test your HTTP service using the ASP.NET MVC Web API.

Run the application by pressing "CTRL+F5". A welcome window will appear as follows:

RunApp.jpgv

In order to call our Product controller, change the URL to "http://localhost:XXXX/api/products". You will see the results as shown in the following output window.

FinalOutput.jpg

The final output returned can be displayed differently by various browsers. Here is the output of Google Chrome version 29.0.1547.66.

Hopefully, this simple web development tutorial will be helpful for developers to code their first HTTP service using the ASP.NET MVC Web API. Further, I'll provide another Web API HTTP service with all Create, Read, Update, and Delete (CRUD) operations in the following weeks.