Using ASP.Net Web API Methods in MVC4

Introduction

In this article, we will define the methods of a Web API service. These methods are GET(),POST(),DELETE() and PUT(). We perform the testing of these methods. Now we define the methods and structure of the URL of methods; they are:

Methods Structure of URL
Get() api/values
GetItem(int i) api/values/i
Post(i) api/values/i and select POST method
Delete(i) api/values/i and select DELETE method

Step 1

First we create the MVC 4 project. And change the name of this project to "app_svc".

mvc.jpg

Step 2

After selecting the project type, we need to select the project template. There are various templates; we can select the WebAPI template.

mudita.jpg

Step 3

On the application go to the Solution Explorer and select the controller, "There is ValuesController". Now we open a file of the ValuesController and we see the five methods GET(), PUT(), DELETE() and POST(),

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. namespace app_svc.Controllers  
  8. {  
  9.     public class ValuesController : ApiController  
  10.     {  
  11.         public IEnumerable<string> Get()  
  12.         {  
  13.             return new string[] { "value1""value2" };  
  14.         }  
  15.         public string Get(int id)  
  16.         {  
  17.             return "value";  
  18.         }  
  19.         public void Post([FromBody]string value)  
  20.         {  
  21.         }  
  22.         public void Put(int id, [FromBody]string value)  
  23.         {  
  24.         }  
  25.         public void Delete(int id)  
  26.         {  
  27.         }  
  28.     }  
  29. }   

Step 4

Now we test the method that is given by the Web API service. We use the Fiddler tool for testing of the methods. Then we download the "FiddlerTool" and installed it. It looks like this:

init.jpg
 

Step 5

Now we modify the methods for the best testing. Here is the code after modification:

  1. using System;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Net;  
  6. using System.Net.Http;  
  7. using System.Web.Http;  
  8. namespace app_svc.Controllers  
  9. {  
  10.     public class ValuesController : ApiController  
  11.     {  
  12.         private List<string>type = new List<string> { "Type1","Type2","Type3","Type4","Type5" };  
  13.         public IEnumerable GetList()  
  14.         {  
  15.             return type;  
  16.         }  
  17.         public string GetItem(int Id)  
  18.         {  
  19.             return type.Find(a => a.ToString().Contains(Id.ToString()));  
  20.         }  
  21.         public List<string> Post(string value)  
  22.         {  
  23.             type.Add(value);  
  24.             return type;  
  25.         }  
  26.         public void Put(int Id, string value)  
  27.         {   
  28.         }  
  29.         public List<string> DeleteItem(int Id)  
  30.         {  
  31.             type.Remove(type.Find((a => a.ToString().Contains(Id.ToString()))));  
  32.             return type;  
  33.         }  
  34.     }  
  35. }  

These are the modifications that are performed in the above:

  • GetList: All the items in a list.
  • GetItem(i): A single item as specified by "i".
  • Post(str): Add the str into the list and display the complete list for the result.
  • Delete(i): It wiil delete the item from the list that is at index "i" and display the result.
Step 6

Now we need to host the API service. Here we host the service at "http://localhost:8081/api/values". If you want to use the GET() method then open Fiddler and select the "Composer" tab and write the URL "http://localhost:8081/api/values" and select the GET method.

Action:

 GET1.jpg

Result:

 val.jpg

Step 7

Here we write the URL http://localhost:8081://api/values/1 and select the Get Method.

Action:

mudita1.jpg
 

Result:

val1.jpg

Step 8

Now we write the URL "http://localhost:8081://api/values/1" and we select the POST method.

Step 9

Here we enter the URL "http://localhost:8081://api/values/1" and select the DELETE method.

Action:

del.jpg

Result:

delf.jpg