In this article, you will learn how to create a MVC Web API and how to consume using JSON.

Why User Web API

Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. The ASP.NET Web API is an ideal platform for building REST applications on the .NET Framework.

Getting Started

Create a new project in Visual Studio 2012 and select ASP.NET MVC 4 Web Application and select Web API template and click OK.

Here is my UserModel Class:

  1. public class UserModel
  2. {
  3. public int UserId { get; set; }
  4. public string UserName { get; set; }
  5. public string UserAddress { get; set; }
  6. public string UserMobile { get; set; }
  7. public string UserCountry { get; set; }
  8. }

Here is my controller code:

  1. using CreateSonsumeWebAPIInMVC.Models;
  2. public class ValuesController : ApiController
  3. {
  4. // GET api/values
  5. public IEnumerable<UserModel> Get()
  6. {
  7. return new UserModel[]
  8. {
  9. new UserModel()
  10. {
  11. UserId= 1,
  12. UserName = "Raj Kumar",
  13. UserAddress = "New Delhi",
  14. UserMobile = "9899461650",
  15. UserCountry = "India"
  16. },
  17. new UserModel()
  18. {
  19. UserId= 2,
  20. UserName = "Mahesh Chand",
  21. UserAddress = "Philadelphia",
  22. UserMobile = "610-484-5670",
  23. UserCountry = "USA"
  24. },
  25. new UserModel()
  26. {
  27. UserId= 3,
  28. UserName = "Stephen Hoffman",
  29. UserAddress = "New York",
  30. UserMobile = "610-484-5680",
  31. UserCountry = "USA"
  32. },
  33. new UserModel()
  34. {
  35. UserId= 4,
  36. UserName = "Deepak Malhotra",
  37. UserAddress = "Brisbane",
  38. UserMobile = "610-487-5680",
  39. UserCountry = "Australia"
  40. },
  41. new UserModel()
  42. {
  43. UserId= 5,
  44. UserName = "John Lehman",
  45. UserAddress = "Johansburg",
  46. UserMobile = "610-187-5680",
  47. UserCountry = "South Africa"
  48. }
  49. };
  50. }
  51. // GET api/values/5
  52. public UserModel Get(int id)
  53. {
  54. return new UserModel()
  55. {
  56. UserId = id,
  57. UserName = "Raj Kumar",
  58. UserAddress = "New Delhi",
  59. UserMobile = "9899461650"
  60. };
  61. }
If you want to see the output, then you need to type the URL like this:

http://localhost:49012/api/values

The result in XML format looks like this:

img1.jpg

Image 1.

http://localhost:49012/api/values/1

img2.jpg

Image 2.

Now let's consume this on a view using JSON.

  1. @section scripts{
  2. <script type="text/javascript">
  3. $(document).ready(function () {
  4. $('#Search').click(function () {
  5. $('#users').empty();
  6. $.getJSON("/api/values", function (data) {
  7. $.each(data, function (i, user) {
  8. alert(i);
  9. //alert(user.UserName);
  10. var content = user.UserId + ' ' + user.UserName;
  11. content = content + ' ' + user.UserAddress + ' ' + user.UserMobile + ' ' + user.UserCountry;
  12. alert(content);
  13. $('#users').append($('<li>', { text: content }, '</li>'));
  14. });
  15. });
  16. });
  17. });
  18. </script>
  19. }
  20. <div>
  21. <div>
  22. <h1>User Listing</h1>
  23. <input id="Search" type="button" value="Get Users" />
  24. </div>
  25. <div>
  26. <ul id="users"></ul>
  27. </div>
  28. </div>

Now run the application to see the output:
img3.jpg

Image 3.