Introduction
In this article, we will discuss how we can convert a console application into an ASP.NET Core API. This article will make your base stronger, and you will gain exact information about the file hierarchy of a Console application project and a web API project. It will become interesting, because we’ll start with a console project and end it with a web API. I am using Visual Studio 2022. You can use what you have. Let’s start.
To convert a console Project into an ASP.NET Core API we need to follow these steps.
Step 1
using ConsoleAppToWebAPI;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using System;
public class Program {
static void Main() {
CreateHostBuilder().Build().Run();
}
public static IHostBuilder CreateHostBuilder() {
return Host.CreateDefaultBuilder().ConfigureWebHostDefaults(webHost => {
webHost.UseStartup < Startup > ();
});
}
}
Adding Another snipped for C#
public static IHostBuilder CreateHostBuilder() {
return Host.CreateDefaultBuilder().ConfigureWebHostDefaults(webHost => {
webHost.UseStartup < Startup > ();
});
}
Adding another One
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
app.UseRouting();
app.UseEndpoints(endpoints => {
endpoints.MapGet("/", async context => {
await context.Response.WriteAsync("Hello from new web Api");
});
});
}