API is an Application Program Interface for either a web server or a web browser. ASP.NET Web API is a framework for building HTTP service for a wide range of devices. WEB API is very similar to ASP.NET MVC. It contains most of the MVC features like routing, controllers, action results, filter, model binders, etc.

Let us create a simple application using the WEB API 2.

Start Visual Studio and go to New Project, then click ASP.NET Web Application.

web

After selecting the option give a name to the web application and click OK. Another dialog box will appear. In that dialog box select Empty web page and check the WEB API option.

empty

Now the Empty web application is created in the project. Then add a class like the the following image.

add

Enter the following model code in the class file.

  1. public class StudentBasicDetails
  2. {
  3. public int stuID
  4. {
  5. get;
  6. set;
  7. }
  8. public string stuName
  9. {
  10. get;
  11. set;
  12. }
  13. public int stuAge
  14. {
  15. get;
  16. set;
  17. }
  18. }
In the above class it has the variable which acts like a data model to get and set the values for the variable.

Now add a new Scaffolded item in the created project.

project

After selectin an item it will open a dialog box which contains a list of Web API and MVC Entity Framework items. In that choose the WEB API 2 Controller - Empty. Here's the screenshot:

empty

Add the following methods in the Web Api 2 controller,
  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 ASPApi
  8. {
  9. public class StudentController: ApiController
  10. {
  11. StudentBasicDetails[] st = new StudentBasicDetails[]
  12. {
  13. new StudentBasicDetails
  14. {
  15. stuID = 1, stuName = "AAA", stuAge = 8
  16. },
  17. new StudentBasicDetails
  18. {
  19. stuID = 2, stuName = "BBB", stuAge = 9
  20. },
  21. new StudentBasicDetails
  22. {
  23. stuID = 3, stuName = "CCC", stuAge = 8
  24. },
  25. new StudentBasicDetails
  26. {
  27. stuID = 4, stuName = "DDD", stuAge = 8
  28. },
  29. new StudentBasicDetails
  30. {
  31. stuID = 5, stuName = "EEE", stuAge = 9
  32. },
  33. new StudentBasicDetails
  34. {
  35. stuID = 6, stuName = "FFF", stuAge = 8
  36. }
  37. };
  38. public IEnumerable < StudentBasicDetails > GetAll()
  39. {
  40. return st;
  41. }
  42. public IHttpActionResultGetStuID(intstuid)
  43. {
  44. var studet = st.FirstOrDefault((s) => s.stuID == stuid);
  45. if (studet == null)
  46. {
  47. return NotFound();
  48. }
  49. return Ok(studet);
  50. }
  51. }
  52. }
In the above controller I have added two methods; one is to return all the values and another one is to display on the specific value according to the input given by the user.

Now we will add a html page to call the API using AJAX. Ajax call can be made by using the jQuery. Go to the Solution Explorer window and right click the project to add new HTML Page like the following screenshot:

add

html

After adding the HTML page replace the HTML code with the following code.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title></title>
  5. <meta charset="utf-8" />
  6. </head>
  7. <body>
  8. <div>
  9. <h4>All Student List</h4>
  10. <ul id="AllStudent" />
  11. </div>
  12. <div>
  13. Student ID:<input id="GetId" type="text" /><input type="button" value="Search" onclick="GetStu();" />
  14. <p id="getStudent"></p>
  15. </div>
  16. <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>
  17. <script>
  18. var link = 'api/Student';
  19. $(document)
  20. .ready(function()
  21. {
  22. $.get(link)
  23. .done(function(data)
  24. {
  25. $.each(data, function(i, val)
  26. {
  27. $("#AllStudent")
  28. .append("<li>Name: " + val.stuName + " Age: " + val.stuAge + "</li>")
  29. });
  30. });
  31. });
  32. function GetVal(studet)
  33. {
  34. return "Name: " + studet.stuName + " , Age: " + studet.stuAge
  35. }
  36. function GetStu()
  37. {
  38. varstuid = $("#GetId")
  39. .val();
  40. $.getJSON(link + '/' + stuid)
  41. .done(function(data)
  42. {
  43. $("#getStudent")
  44. .text(GetVal(data))
  45. })
  46. .fail(function()
  47. {
  48. alert("data not found");
  49. });
  50. }
  51. </script>
  52. </body>
  53. </html>

You can view the code from the GitHub link.

Final Output

output

Read more articles on ASP.NET: