Creating First HTTP Service using ASP.NET Web API: Part1 of Many

In this post I will give you basic understating of creating HTTP Services using ASP.NET Web API. Here I am assuming that you are aware of basic ASP.NET MVC.

To work with ASP.NET Web API, you need to install ASP.NET MVC 4.0. Download ASP.NET Web API from here and install it from Web Installer.

After installation create a new project choosing ASP.NET MVC 4 Web Application as project type.

image

Next choose Web API as template.

image

On examining project structure you will find it is exactly as usual ASP.NET MVC project structure. Let us go ahead and add a MODEL. For that right click on the Model folder and add a class. Let us add a model called Blogger

Blogger.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace firstwebapiapplication.Models
{
public class Blogger
{
public int Id { get; set; }
public string Name { get; set; }
public string Intrest { get; set; }
}
}

After adding Model let us go ahead and add Controller. There are two differences between Controller of usual ASP.NET MVC Controller and ASP.NET Web API Controller.

  • ASP.NET Web API Controller returns data not view
  • ASP.NET Web API Controller is derived from ApiController class.

Now go ahead and add a Controller called BloggerController. For that right click on Controller folder and add a controller.

image

Choose Empty Controller as Scaffolding options.

image

Next you need to change the class controller is inheriting. Change Controller to ApiController.

image

Controller class needs to inherit ApiController class from System.Web.Http namespace. Let us add a function to return information about Bloggers.

BloggerController.cs


using System.Collections.Generic;
using firstwebapiapplication.Models;
using System.Web.Http;

namespace firstwebapiapplication.Controllers
{
public class BloggersController : ApiController
{
//
// GET: /Bloggers/



public IEnumerable<Blogger> GetAllBloggers()
{
return new List<Blogger>
{
new Blogger(){ Id= 1,Name="DebugMode",Intrest="ASP.NET Web API"},
new Blogger(){ Id= 2,Name="KodefuGuru",Intrest ="C Sharp"},
new Blogger(){ Id= 3,Name="ActiveNick",Intrest = "Windows Phone" },
new Blogger(){ Id= 4,Name= "GBLOCK ", Intrest ="REST"},
new Blogger(){ Id= 5,Name = "WadeWegner",Intrest = "Windows Azure " }
};
}

}
}


By this point you have created HTTP Service. Press F5 to host the service on Cassini server.

clip_image001

And you will get a file to download

clip_image003

Next we can consume it in any client. In later post I will show you to consume in JavaScript client and Windows Phone as well. I hope this introductory blog post is useful to get started with ASP.NET Web API. In next post we will get into consuming ASP.NET WEB API Services in various clients

 

About these ads

4 thoughts on “Creating First HTTP Service using ASP.NET Web API: Part1 of Many

  1. Pingback: Consuming ASP.NET Web API Service using HttpClient: Part2 of Many « debug mode……

  2. Pingback: Monthly Report March 2012: Total Posts 15 « debug mode……

  3. Pingback: How to Self-Host ASP.Net Web API: Part 3 of Many « debug mode……

  4. Pingback: ASP.Net Web API Service in Windows Phone: Part 4 of Many « debug mode……

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s