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

 

How to Create Bootable USB for Windows 8 Consumer Preview

In this post I will share screens to create Bootable USB for Windows 8 Consumer Preview.

Windows 8 Consumer Preview is available to Download

After downloading Windows 8 Consumer Preview you need to download

Windows 7 USB/DVD Download Tool

After downloading unzip and start running the setup. Follow the screens as below,

image

image

image

After installing makes sure that you have 4GB USB plugged in to the system.

clip_image001

Once Windows7 USB/DVD Download tool is installed open it

clip_image002

Browse to location where you downloaded ISO Image of Windows8 Consumer Preview

clip_image003

Follow the screens to copy ISO on bootable USB.

clip_image004

clip_image005

From dropdown choose the USB where you want to copy and select Begin Copying.

clip_image006

Choose option Erase USB Device.

clip_image007

Select option Yes

clip_image008

clip_image009

After successful copying you should be getting the below screen.

clip_image010

Now change boot setting in BIOS and make USB as primary booting device and start installing Windows 8 Consumer on your machine.