How To Create a Downloadable Web API

Introduction

In this article we will describe how to create a downloadable Web API. Since we know that an API is a framework that can help to create HTTP services to provide the responses of the client request. HTTP services reaches a broad range of clients and also includes the browsers and mobile services. Here we describe the calling of the API application  from the browser.

Step 1

Create the Blank Solution

  • Open Visual Studio 2012
  • Select "File" -> "New" -> "Project...".
  • On the template window select "Installed" > "Template" > "Other Project Type" > "Visual Studio Solution".
  • Now choose "Blank Solution" and change the name of this solution to "HelloSolution".
  • Now click on the "OK" button.
    sol1.jpg
Step 2

Create ASP. Net MVC Web API application

After creating the blank solution we can now add an ASP.Net Web API project.

  • Right-click on the solution "HelloSolution" in the Solution Explorer.
  • Select "Add" > "New Project..."
  • In the Installed Templates expand the "Visual C#" node and select "ASP. Net MVC 4 Application".
  • Click on "OK" button.
  • Select the Web API from the New ASP.Net Web API.

    sol3.jpg

    sol4.jpg

    Solution Explorer image

    sol5.jpg

Step 3

Add the Controller

Now use the following procedure to create the Controller:

  • Right-click on the Controller Folder in the Solution Explorer. Select "Add" > "Controller".
  • In the controller window change the name to "helloController".
  • And in the template select Empty API controller.
  • Click on the "Add" button.
    sol6.jpg

    sol7.jpg

Step 4

Write the following code in the helloController.cs:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. namespace helloAPI.Controllers  
  8. {  
  9.     public class helloController : ApiController  
  10.     {  
  11.         public string Get()  
  12.         {  
  13.             return "Mudita Rathore";  
  14.         }  
  15.         public string Get(string Id)  
  16.         {  
  17.             return "Mudita " + Id;  
  18.         }  
  19.     }  
  20. } 

Step 5

Now to call the Web API from the browser.

For calling the Web API, first we debug the code, then the result will look like this:

 exe1.jpg

Now we access the URL "http://localhost:48909/api/hello".

 exe2.jpg

Now we click on "Open" and select the browser to access the Web API.

Output

The result will look like this in the Mozilla Firebox browser:

res1.jpg


Similar Articles