Sunday, September 8, 2013

Getting Started with WebApis

It’s been great Sunday morning so far, waking up early really a great deal. This will be a quick into to the WebApi, then will have some gym thereafter.
In the previous post we have have seen what is REST, why it is there and why I’ve started using REST architecture at the first place. Now in this post we will see how can we implement REST using WebApi.
If you remember, REST suggests using the basic HTTP verbs (GET POST UPDATE and DELETE) and also in REST everything is accessed by URL.

For instance consider the following code.

public class HelloClass
{ 
   public string SayHello()
   {
       return "Hello World";
   }
}

REST says, if you want to access the method SayHello(), you must write something like,
http://localhost/api/HelloClass/SayHello
Which will return you a JSON formatter string saying “Hello World”.
However, with normal classes, this is not possible. We need to have special classes that can be serialized into JSON or XML etc. So, Let’s see how it can be done.

Creating WebApi’s Hello World Application

Create new project and select MVC 4 application template.

SNAGHTML65cbf76f

Give the application a name and click ok. An MVC 4 project template will open.

SNAGHTML65cdc99c

Select WebAPI and click ok. When visual studio finishes creating all, we will be presented with a ValuesController.cs class file, and this is where the magic happens. Here is the code of the class ValuesController.cs.

public class ValuesController : ApiController
{
    // GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
 
    // GET api/values/5
    public string Get(int id)
    {
        return "value";
    }
 
    // POST api/values
    public void Post([FromBody]string value)
    {
    }
 
    // PUT api/values/5
    public void Put(int id, [FromBody]string value)
    {
    }
 
    // DELETE api/values/5
    public void Delete(int id)
    {
    }
}

As one can see, the class ValuesController inherits ApiController class which is responsible for changing the behavior of a class from normal to REST Architecture based. Now don’t give a mess to this class, just hit F5 and execute the code. Once the application is running, we are going to call the GET function via Url as we were discussing lately. Append the following line to the application url:
http://localhost:42844/api/Values

SNAGHTML65defae8[4]

SNAGHTML65dff119

On navigating to the URL the internet explorer will ask you if you want to download the values.json file. Save the file somewhere in your hard drive.

SNAGHTML65e27bbb

Once saved, open the file in the notepad, you can see the result returned as ["value1","value2"] which is what we have returned from the first Get function of our ValuesController class.

I hope this post will be useful, in the next post will be discussing some deeper stuffs. Somehting other than HelloWorld.

Stay tuned.
Happy Reading.

Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top