View Count : web counter free

In this article we will see how to create a simple Asp.Net Web API service and host it on IIS

Creating Our First Asp.Net Web API

Start VS2013 --> Just goto File --> New --> Project and Select ASP.NET Web Application and I will name it as MyFirstWebAPI

We will do everything from scratch. We will start an empty WebAPI project and click OK.

It is good to have basic understanding of MVC (or Learn Here For Free) before proceeding for ASP.NET Web APIs. If you do not have, even then no worries! it is very simple.

The directory structure is same as MVC project. You have Controllers and Models.

Now I am going to add a new Controller. I will say Right Click on Controllers folder and Add --> Controller and I am going to add ASP.NET Web API 2 Controller which is Empty.

Now, what is the difference between Web API and Web API 2? Web API2 of Web API, because you are a newbie to Web API you need not worry much about the versions.

So for a newbie there is no much difference in various versioning’s of Web API. So as of now let us go with Web API 2 Controller - Empty.

I will name it as MTTController and I will Add.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace MyFirstWebAPI.Controllers
{
public class MTTController : ApiController
{

}
}

Now, if you observe your Web API Controller is a class which inherits from a pre-defined class that is ApiController, so your Web API Controller is nothing but a class in simple term which inherits from ApiController (which is a pre-defined class).

Now, I will define a method which returns string and I will name it as Get(). As we know that it follows certain conventions. So for GET method your method name should be Get and it is going to return “ManzoorTheTrainer”.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace MyFirstWebAPI.Controllers
{

public class MTTController : ApiController
{

// ../api/MTT
public string Get()
{
return "ManzoorTheTrainer.com";
}

}

}

Now, how do I access this? MyBaseAddress/api/MTT (Controller Name).That’s it.
Because by default my method from the browser is GET. If I just type in “/api/MTT” it is going to Invoke Get method. Now, let me execute this.

My base address is http://localhost:4070 and I will say http://localhost:4070/api/MTT and you see that it returns “ManzoorTheTrainer”.

I will say F12. Now if you see your request, in its Headers the Request Method Is GET and Status Code is 200 it means OK. Whatever you were expecting it has returned it.

Now what if I want to return an array. I need to simply use string[] as return type and I will say return new array of string, Save this. Now let me execute it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace MyFirstWebAPI.Controllers
{

public class MTTController : ApiController
{

// ../api/MTT
public string[] Get()
{
return new string[] { "ManzoorTheTrainer", "asp" };
}

}

}

Again I will call /api/MTT. Now if you see that it is returning me an array of string.

Now I will try to enhance a little. Here I am going to declare a private variable and return S;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace MyFirstWebAPI.Controllers
{

public class MTTController : ApiController
{
private string[] S = new
string[] { "ManzoorTheTrainer", "asp" };

// ../api/MTT
public string[] Get()
{
return S;
}

}

}

Let me execute. It should give me the same result.

Now I am going to define another GET method. At this time I am expecting int id and I am going to return a single string of that index.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace MyFirstWebAPI.Controllers
{

public class MTTController : ApiController
{
private string[] S = new
string[] { "ManzoorTheTrainer", "asp" };

// ../api/MTT
public string[] Get()
{
return S;
}

// ../api/MTT/id
public string Get(int id)
{
return S[id];
}

}

}

Let me execute and see what it says if I say /api/MTT it should return me both the things

And if I say /api/MTT/0 it returns ManzoorTheTrainer

And if I say /api/MTT/1 it returns asp.

Put a breakpoint. It understands very well, what method is being called. If it is /api/MTT it is going to jump to the Get() method. If is /api/MTT/{an id} (Ex: /api/MTT/1) then is going to jump Get() method with parameter id 1 and it is going to return asp. This method is invoked with parameter id.

So that’s it, we are ready with a simple ASP.NET Web API Service. Now let us see how to host it on IIS.

Hosting Asp.Net Web API On IIS

Step-1: So, I can simply go to inetmgr.

Step-2: Navigate to Sites. Right click on Default Web Site and say Add Virtual Directory.

Step -3: Alias it as MyFirstAPI. Select the physical path for MyFirstWebAPI project and say OK.

Step-4: Right Click on MyFirstAPI and Convert to Application and say OK.

Step-5: Right click on MyFirstAPI --> Manage Application --> Browse.

Step-6: In the URL I will say localhost/MyFirstAPI/api/MTT and if you see that I get both the strings.

And if I say localhost/MyFirstAPI/api/MTT/1, I get asp.

If I say localhost/MyFirstAPI/api/MTT/0, 0 is the index, I get ManzoorTheTrainer

Instead of localhost I can use my system name or server name that is thinkpad-pc.

It means that my Web API is up and running. Now I don’t need the Visual Studio running anymore because it is on my local IIS.

Now how do I use it in a client? Let us see that in our next article(Coming Soon).

Good News: This article has been highlighted as "Article Of The Day" on 23-11-2016 at https://asp.net/community/articles