In this article, you will learn how to consume/use a Web API in ASP.NET MVC step by step.
This article is the second part of my ASP.NET MVC Web API series. In Part One, we discussed how to create a Web API and perform CRUD operations. You can find that article at the below link.
Consuming Web API
In this article, we have used the localhost for Web API and called the GET request.
- Create ASP.NET MVC Project
- Add MemberViewModel
- Add Microsoft.AspNet.webApi.Client from the NuGet library
- Code for consuming the Web API
- Run the project and call action method on URL
Step by step implementation:
Step 1
Create a new ASP.NET MVC Web Application project called “ConsumeWebApiMVC”.
The default project files in the Solution Explorer look like the following.
Now, create a ViewModel called MemberViewModel inside the Models folder.
Right-click on Models folder. Go to Add --> New Item or press CTRL + SHIFT + A.
MemberViewModel.cs Code
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace ConsumeWebApiMVC.Models
- {
- public class MemberViewModel
- {
- public int MemberID { get; set; }
- public string MemberName { get; set; }
- public string PhoneNumber { get; set; }
- }
- }
You can see HomeControllers inside the Controllers folder. Double-click on HomeControllers and add the following line for getting ViewModel listing in the controller.
- using ConsumeWebApiMVC.Models;
Now, add Microsoft.AspNet.webApi.Client from the NuGet library.
Right-click on References folder and select it.
Click OK as in the above dialog box to install Microsoft.AspNetWebApi.Client.
Click on "I Accept" to confirm the installation again.
You can see in the output window that the NuGet package is successfully installed.
After adding the NuGet package, now, let us code for consuming the Web API and calling the get method of all members' records.
- public ActionResult GetMembers()
- {
- IEnumerable<MemberViewModel> members = null;
-
- using (var client = new HttpClient())
- {
- client.BaseAddress = new Uri("http://localhost:52044/api/");
-
-
-
-
- var responseTask = client.GetAsync("member");
- responseTask.Wait();
-
-
- var result = responseTask.Result;
-
-
- if (result.IsSuccessStatusCode)
- {
- var readTask = result.Content.ReadAsAsync<IList<MemberViewModel>>();
- readTask.Wait();
-
- members = readTask.Result;
- }
- else
- {
-
- members = Enumerable.Empty<MemberViewModel>();
- ModelState.AddModelError(string.Empty, "Server error try after some time.");
- }
- }
- return View(members);
- }
- var responseTask = client.GetAsync("member");
The following are the methods of HttpClient:
SR. NO.
|
HTTP CLIENT METHODS
|
DESCRIPTION
|
1 |
Client.GetAsync |
To send GET request. |
2 |
Client.PostAsync |
To send POST request. |
3 |
Client.PutAsync |
To send PUT request. |
4 |
Client.DeleteAsync |
To send DELETE request. |
Now, add a View for GetMembers. Right-click on GetMembers Action Method.
After clicking on "Add View" option, fill the Add View form.
Then, click the Add button to proceed further.
Now, you can see GetMembers.cshtml file created inside VIEWS --> HOME folder.
Now, let us check our Web API code by pressing F5.
http://localhost:50604/home/getmembers
In the next article, you will learn how to call Web API through jQuery.
Happy Coding.