Web APIs (Application Programming Interfaces) are an essential part of modern web development. They allow different applications to communicate and share data with each other. In this tutorial, we will explore how to build and consume Web APIs using C#.

Building a Web API in C#
To build a Web API in C#, we can make use of the ASP.NET Core framework. Follow the steps below to create a simple Web API:

1. Create a new ASP.NET Core project: Open Visual Studio and select “Create a new project”. Choose “ASP.NET Core Web Application” and click “Next”. Give your project a name and location, and click “Create”. In the next window, select “API” as the project template.

2. Define the API endpoints: In the newly created project, open the “Controllers” folder. Here, you can create a new class that will define your API endpoints. For example, create a file named “ValuesController.cs” and add the following code:

“`csharp
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace MyAPI.Controllers
{
[Route(“api/[controller]”)]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult> Get()
{
return new string[] { “value1”, “value2” };
}

// GET api/values/5
[HttpGet(“{id}”)]
public ActionResult Get(int id)
{
return $”value{id}”;
}

// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}

// PUT api/values/5
[HttpPut(“{id}”)]
public void Put(int id, [FromBody] string value)
{
}

// DELETE api/values/5
[HttpDelete(“{id}”)]
public void Delete(int id)
{
}
}
}
“`

This class defines an `ApiController` with the `[Route(“api/[controller]”)]` attribute, which sets the route prefix for all the endpoints in this controller to “/api/values”. The various methods define the HTTP verbs (`HttpGet`, `HttpPost`, etc.) and the corresponding endpoint URLs (`/api/values`, `/api/values/{id}`, etc.).

3. Run the API locally: You can now run the API locally by clicking the “Start” button in Visual Studio or pressing F5. This will launch a local web server hosting your API.

4. Test the API endpoints: You can test the API endpoints using a tool like Postman or by directly accessing the endpoints in a web browser. For example, to test the `GET api/values` endpoint, you can navigate to `http://localhost:port/api/values` in your web browser or send a GET request to that URL using Postman.

Consuming a Web API in C#
To consume a Web API in C#, you can use the `HttpClient` class from the `System.Net.Http` namespace. Follow the steps below to consume the Web API you just created:

1. Create a new C# console application project in Visual Studio.

2. Install the `Microsoft.AspNet.WebApi.Client` NuGet package: Right-click on the project in the Solution Explorer and select “Manage NuGet Packages”. Search for `Microsoft.AspNet.WebApi.Client` and click “Install” to add the package to your project.

3. Add the following code to the `Program.cs` file in your console application project:

“`csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace MyConsoleApp
{
class Program
{
static async Task Main(string[] args)
{
using (HttpClient client = new HttpClient())
{
// Send a GET request to the Web API endpoint
HttpResponseMessage response = await client.GetAsync(“http://localhost:port/api/values”);
response.EnsureSuccessStatusCode(); // Throw an exception if the response is not successful

// Get the response content as a string
string responseBody = await response.Content.ReadAsStringAsync();

// Display the response
Console.WriteLine(responseBody);
}
}
}
}
“`

The `HttpClient` class is used here to send HTTP requests and receive HTTP responses. In this example, we are sending a `GET` request to the `http://localhost:port/api/values` endpoint and printing the response body.

4. Run the console application: You can run the console application by clicking the “Start” button in Visual Studio or by pressing F5. This will execute the code and display the response from the Web API in the console window.

In this tutorial, we learned how to build a simple Web API using ASP.NET Core and how to consume that API using a C# console application. Web APIs are a powerful tool for building scalable and loosely coupled applications, and C# provides robust libraries and frameworks for both building and consuming APIs.