Login Register
Code2night
  • Home
  • Blog Archive
  • Tutorial
  • Interview Q&A
  • Languages
    • Angular
    • C
    • c#
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • Security
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
    • SEO Analyzer
  1. Home
  2. Blog
  3. Calling Web api from Server Side using Asp.Net Core

Calling Web api from Server Side using Asp.Net Core

Date- Feb 16,2023

7454

Free Download Pay & Download
HttpClient Calling web api

"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!


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.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

How to Encrypt and Decrypt Password in Asp.Net
May 15, 2022
Exception Handling Asp.Net Core
Aug 05, 2020
HTTP Error 500.31 Failed to load ASP NET Core runtime
Aug 23, 2022
How to implement Paypal in Asp.Net Core
Oct 30, 2022

Comments

Contents

More in ASP.NET Core

  • Task Scheduler in Asp.Net core 17494 views
  • Implement Stripe Payment Gateway In ASP.NET Core 16744 views
  • Send Email With HTML Template And PDF Using ASP.Net C# 16508 views
  • How to implement Paypal in Asp.Net Core 8.0 12884 views
  • Import data from Excel in Asp.Net 12743 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
  • SEO Analyzer
By Language
  • Angular
  • C
  • c#
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page