
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to consume Asp.Net WebAPI endpoints from other applications using C#?
HttpClient class provides a base class for sending/receiving the HTTP requests/responses from a URL. It is a supported async feature of .NET framework. HttpClient is able to process multiple concurrent requests. It is a layer over HttpWebRequest and HttpWebResponse. All methods with HttpClient are asynchronous. HttpClient is available in System.Net.Http namespace.
Let us create an WebAPI application having a StudentController and respective action methods.
Student Model
namespace DemoWebApplication.Models{ public class Student{ public int Id { get; set; } public string Name { get; set; } } }
Student Controller
using DemoWebApplication.Models; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace DemoWebApplication.Controllers{ public class StudentController : ApiController{ List<Student> students = new List<Student>{ new Student{ Id = 1, Name = "Mark" }, new Student{ Id = 2, Name = "John" } }; public IEnumerable<Student> Get(){ return students; } public Student Get(int id){ var studentForId = students.FirstOrDefault(x => x.Id == id); return studentForId; } } }
Now, let us create a console application where we want consume the above created WebApi endpoints to get the student details.
Example
using System; using System.Net.Http; namespace DemoApplication{ public class Program{ static void Main(string[] args){ using (var httpClient = new HttpClient()){ Console.WriteLine("Calling WebApi for get all students"); var students = GetResponse("student"); Console.WriteLine($"All Students: {students}"); Console.WriteLine("Calling WebApi for student id 2"); var studentForId = GetResponse("student/2"); Console.WriteLine($"Student for Id 2: {students}"); Console.ReadLine(); } } private static string GetResponse(string url){ using (var httpClient = new HttpClient()){ httpClient.BaseAddress = new Uri("http://localhost:58174/api/"); var responseTask = httpClient.GetAsync(url); var result = responseTask.Result; var readTask = result.Content.ReadAsStringAsync(); return readTask.Result; } } } }
Output
Calling WebApi for get all students All Students: [{"Id":1,"Name":"Mark"},{"Id":2,"Name":"John"}] Calling WebApi for student id 2 Student for Id 2: {"Id":2,"Name":"John"}
In the above example we could see that the endpoints of the WebApi is called from a separate console application.
- Related Articles
- How to use ViewBag in ASP .Net MVC C#?
- What is ViewData in ASP .Net MVC C#?
- How to extract data from SAP using .NET provider.
- What is the significance of NonActionAttribute in ASP .Net MVC C#?
- What is the use of ChildActionOnly attribute in ASP .Net MVC C#?
- Can Selenium be used for .NET applications?
- How can we provide an alias name for an action method in Asp .Net MVC C#?
- Upgrading SAP .NET Connector from .NET 2.0 to .NET 3.0
- How to calculate net present value using Net present value (NPV)?
- How to share app data with other applications in android?
- What are the levels at which filters can be applied in ASP .Net MVC C#?
- What are the three segments of the default route, that is present in ASP .Net MVC\nC#?
- How to get formatted JSON in .NET using C#?
- How to capture video from other cameras in OpenCV using C++?
- How to return custom result type from an action method in C# ASP.NET WebAPI?

Advertisements