Simple web api - dot net core -minimal api

 Here's a simple example of a ASP.NET Core Web API and how it's called from another ASP.NET Core web application, highlighting any differences between .NET 6 and .NET 8:

1. ASP.NET Core Web API (WebApiProject)

This project defines a simple API controller with a GET endpoint that returns a message.

WebApiProject/Controllers/ValuesController.cs (both .NET 6 & .NET 8):

Code snippet
using Microsoft.AspNetCore.Mvc;

namespace WebApiProject.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // GET: api/values
        [HttpGet]
        public string Get()
        {
            return "Hello from Web API!";
        }
    }
}

2. ASP.NET Core Web Application (WebAppProject)

This project calls the Web API endpoint and displays the returned message.

Minimal API approach (recommended for .NET 6 & .NET 8):

WebAppProject/Program.cs (both .NET 6 & .NET 8):

Code snippet
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Net.Http;

namespace WebAppProject
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            // Call the Web API endpoint
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:your_port/api/");
                var response = await client.GetAsync("values");
                string message = await response.Content.ReadAsStringAsync();
                Console.WriteLine(message);
            }

            await host.RunAsync();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

Explanation:

  1. The ValuesController in WebApiProject defines a simple GET endpoint that returns a string.
  2. The Program.cs code in WebAppProject uses HttpClient to call the Web API endpoint in a Main method (suitable for console applications).
  3. It sets the base URL of the Web API and performs an asynchronous GET request.
  4. The code reads the response content as a string and displays it on the console.

Calling from a Blazor component (applicable to .NET 6 & .NET 8):

WebAppProject/Pages/Index.razor (Blazor component):

HTML
@page
@inject HttpClient Http

<h1>Call Web API</h1>
<p>@message</p>

@code {
    private string message;

    protected override async Task OnInitializedAsync()
    {
        Http.BaseAddress = new Uri("http://localhost:your_port/api/");
        var response = await Http.GetAsync("values");
        message = await response.Content.ReadAsStringAsync();
    }
}

Explanation:

  1. This Blazor component injects HttpClient and calls the Web API endpoint in the OnInitializedAsync method.
  2. It sets the base URL and performs an asynchronous GET request.
  3. The code reads the response content as a string and displays it in a paragraph element.

Differences between .NET 6 and .NET 8:

There are no significant differences in code structure or calling the Web API between .NET 6 and .NET 8 for this basic example. Both versions support the Minimal API approach and Blazor integration.

Running the example:

  1. Create two separate projects (WebApiProject and WebAppProject) using the ASP.NET Core Web API and ASP.NET Core Web Application templates (choose Minimal API for .NET 6 or later).
  2. Replace "your_port" in the code with the actual port your WebApiProject is running on (e.g., 5000).
  3. Run the WebApiProject first.
  4. Run the WebAppProject (console app or Blazor web app).
  5. You should see "Hello from Web API!" displayed in the console or Blazor UI.

This is a basic example. You can adapt it to your specific needs, including:

  • Different API endpoints with various HTTP methods.
  • Passing data to the API endpoint in the request body.
  • Handling different

Post a Comment

Previous Post Next Post