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.






Abhay TiwariPosted Mar 12, 2024, 1:34 AM
Good explanation Sir.
Sujit PrabhakaranPosted Mar 14, 2023, 1:54 PM
Amazing this works to invoke the api!
Roo ViPosted Feb 5, 2021, 11:41 AM
Hi, your articles are very informative. Can you please include the authentication process too where users need to be authenticated from web api to get access of whole application system.
ivan putraPosted Apr 12, 2020, 9:45 PM
Thanks brother.. appreciate it very much
vishal gautamPosted May 21, 2019, 9:51 AM
Nicely explain thank you
Rajan MishraPosted Feb 11, 2019, 2:41 AM
Simple and easy
Alice NguyenPosted Nov 28, 2018, 10:39 PM
Hi,Check the Web API code by Manoj Kalla, no data view I have modified the code in controller / home, as follows: 1. client.BaseAddress = new Uri ("http: // localhost: 9096 /"); 2. Var varTask = client.GetAsync ("api / categories"); Currently Done ... Thanks, Manoj Kalla
Alice NguyenPosted Nov 15, 2018, 8:18 PM
Thank for sharing, very nice article
Carl AndresPosted Oct 1, 2018, 10:22 AM
With these I'm confused where to put some business logic and validation: in the API or the MVC projejct models. I think it should be on the API so it will be centered across multiplatform.
Hadshana KamalanathanPosted Aug 26, 2018, 11:26 AM
Nice article , thanks for sharing
David GlassPosted Aug 25, 2018, 11:11 AM
The use of .Wait here is pretty awful and you're asking for deadlocks.
Rajesh KumarPosted Aug 24, 2018, 6:32 AM
Nice article..............