Calling Web api from Server Side using Asp.Net Core
"How To Call Web API From ASP.NET Core" - that's what we will cover in this blog post. So sit back, relax, and get ready to learn!
Hello guys and welcome to Code2Night! If you are a developer working with C# or Asp.Net Core, you may find yourself in a situation where you need to consume web APIs from server-side applications. In this article, we will explore how to accomplish this using Asp.Net Core and the HttpClient class.
Web APIs are becoming increasingly important for modern software development. They allow different systems to communicate and share data, making it easier to integrate different components into a larger system. As a result, developers need to have a good understanding of how to call web APIs from server-side applications.
The good news is that Asp.Net Core provides a powerful and easy-to-use framework for consuming web APIs. One of the key components of this framework is the HttpClient class, which makes it simple to send HTTP requests to a web API and receive the response.
In this article, we will go through the process of calling a web API from an Asp.Net Core application using HttpClient. We will cover everything you need to know, from setting up your project to making the actual request and handling the response.
Whether you are a seasoned developer or just starting out, this article will provide you with the knowledge and tools you need to call web APIs from Asp.Net Core. So let's get started and learn how to call web API from Asp.Net Core!
HttpClient
For calling any web API from the server side in Asp.Net we can use HttpClient. it basically asks for the Base address where you have hosted the API and then the API URL and you can pass parameters using c# and thus can consume the API
For using this follow the steps
Copy the following code where you want to call the API from asp.net. Install the Newtonsoft.Json Nuget package in your application
using System.Net.Http;
using Newtonsoft.Json;
public async Task<IActionResult> Index() { try { Department obj = new Department(); using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://localhost:44347"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await client.GetAsync("api/Department/GetDepartment?id=1"); List<KeyValuePair<string, string>> values = new List<KeyValuePair<string, string>>() { new KeyValuePair<string, string>("DepartmentId", "1"), new KeyValuePair<string, string>("DepartmentName", "test") }; FormUrlEncodedContent requestContent = new FormUrlEncodedContent(values); HttpResponseMessage response1 = await client.PostAsync("api/Department/SetDepartment", requestContent); if (response.IsSuccessStatusCode) { var result = response.Content.ReadAsStringAsync().Result; obj = JsonConvert.DeserializeObject<Department>(result); } //Send HTTP requests from here. } } catch(Exception ex) { } return View(); }
Here in the BaseAddress, you have to add the URL where your API is hosted. In client.GetAsync we have to add the API URL. After this run the application and it will hit the API from c#.
So this is how we can implement Calling Web API from Server Side using Asp.Net Core.