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".
Step 2
After selecting the project type, we need to select the project template. There are various templates; we can select the WebAPI template.
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(),
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- namespace app_svc.Controllers
- {
- public class ValuesController : ApiController
- {
- public IEnumerable<string> Get()
- {
- return new string[] { "value1", "value2" };
- }
- public string Get(int id)
- {
- return "value";
- }
- public void Post([FromBody]string value)
- {
- }
- public void Put(int id, [FromBody]string value)
- {
- }
- public void Delete(int id)
- {
- }
- }
- }
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:
Step 5
Now we modify the methods for the best testing. Here is the code after modification:
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- namespace app_svc.Controllers
- {
- public class ValuesController : ApiController
- {
- private List<string>type = new List<string> { "Type1","Type2","Type3","Type4","Type5" };
- public IEnumerable GetList()
- {
- return type;
- }
- public string GetItem(int Id)
- {
- return type.Find(a => a.ToString().Contains(Id.ToString()));
- }
- public List<string> Post(string value)
- {
- type.Add(value);
- return type;
- }
- public void Put(int Id, string value)
- {
- }
- public List<string> DeleteItem(int Id)
- {
- type.Remove(type.Find((a => a.ToString().Contains(Id.ToString()))));
- return type;
- }
- }
- }
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:
Result:
Step 7
Here we write the URL http://localhost:8081://api/values/1 and select the Get Method.
Action:
Result:
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:
Result: