Create HTTP Service Using ASP.Net Web API

The software requirements for creating a Http service using ASP.NET web API are:

  • Visual Studio 2010 or Visual Studio 2012 RC 
  • MVC 4.0
In this article I am using Visual Studio 2012 RC.

 

Steps for creating Web API Project

Start Visual Studio 2012 RC and select New Project from the Start page. Or, from the File menu, select New and then Project.

Select Installed Templates and expand the Visual C# node. Under Visual C#, select Web. In the list of project templates, select ASP.NET MVC 4 Web Application. Name the project "SampleMvcWebAPI" and click OK.

Image1.png

After that in the list of project templates select the Web API template.

Image2.png

Now you will have the basic architecture of a MVC (Model, View and Controller) Project. If you will expand the controller then you will see that there are already two controllers available.

  1. HomeController : This is a Traditional controller which inherits the Controller class and returns the view. This Controller is not related to the Web API
  2. ValueController : This Controller is related to the Web API; it inherits the ApiController class and returns data instead of the view.

Image3.png

So for learning purposes I am going to create a UserController, in which I'll create one action for getting all users.

So first I'll create one model class for the user. Let's right-click on the model then select "Add" then select "class":

Image4.png

Give the class the name "User.cs" and press ADD:

Image5.png

And use the following code for the User.cs class:

 public int ID { get; set; }
 
public string userName { get; set; }
 
public string userAddress { get; set; }
 
public string phone { get; set; }

Now our model is ready, so now it's time to create a new Controller.

Right-click on Controllers and select ADD then controller:

Image6.png

For the Controler name specify "UserController" and for the Scaffolding options Template select "Empty API controller" then press the "Add" button.

Image7.png

In this controller you can add an action for Get, Post, Put and Delete. In this article I am going to define only a Get action for fetching all users.

Include the namespace of your model in the UsersController class.

using SampleMvcWebAPI.Models;

For the demo purpose I have defined one User type array in the controller class and one get method to get all users:

 using System;
 
using System.Collections.Generic;
 
using System.Linq;
 
using System.Net;
 
using System.Net.Http;
 
using System.Web.Http;
 
using SampleMvcWebAPI.Models;
 
namespace SampleMvcWebAPI.Controllers
 {
    
 
public class UsersController : ApiController
 
    {
        
 
User[] users = new User[]
 
        {
            
 
new User { ID = 1, userName = "Manish", userAddress= "Noida", phone="1234566" },
            
 
new User { ID = 2, userName = "Nipun", userAddress = "Delhi", phone = "78845545"},
            
 
new User { ID = 3, userName = "Gaurav", userAddress = "Delhi", phone = "6456445" }
 
        }; 
        
 
public IEnumerable<User> GetAllUsers()
 
        {
            
 
return users;
 
        }
 
    }
 }


Now your web API is ready. Let's build the application by pressing CTRL + F5.

You will get output like:

Image8.png

This home page is an ASP.NET MVC view, returned by the HomeController class. To invoke the web API, type this URI into your browser:

http://localhost:1234/api/Users

Here 1234 is the port number; this will give you a complete list of users like that.

The exact result depends on which web browser you are using. Internet Explorer will prompt you to open or save a "file" named users. That is in the Json format.

Image9.png

And the Mozilla browser returns the response in XML Format.

Image10.png

In this article you have learned how to create a HTTP service using the Web API. In this article the service URI is http://localhost:1234/api/Users.

That's All.

Up Next
    Ebook Download
    View all
    Learn
    View all