In this Video, I am going to discuss How to Build ASP.NET Core Web API Project from Scratch i.e. how to convert a console application to a Web API Application. In our previous Video, we discussed the default Web API template and, in this Video, we are going to discuss what are the component and how they are working behind the scene. So, what we will do here in this Video, we will create an ASP.NET Core Console Application, and then we will convert that console application into ASP.NET Core Web API Application step by step.
How to Build ASP.NET Core Web API Project from Scratch?. The Complete ASP.NET Core Web API Developer Course 2023 [Videos].
Converting Console Application to Web API Application:
In the case of ASP.NET Core, by default, each .NET Core Application is a Console Application. This is the reason why you can see by default the Program.cs class file with the Main method in each .NET Core Web App. But then we need to add some services (specific to application type) and also need to do some changes to convert the console application to a Web API Project.
Create a new Console Application
Open Visual Studio and then click on the Create a new Project option as shown in the below image.
Then from the next Create a Project window, select Console Application that targets .NET Core Framework and click on the Next button as shown in the below image.
From the next Configure your new project window, give a meaningful name to your project (I am giving the name ConsoleToWebAPI), and select the location where we want to create the console application. Then if you want you can give a different name to your solution but I am going with the default one. Finally, click on the Next button as shown in the below image.
From the next Additional Information window, select the target .NET Core Framework. As we are working with .NET 5, so select .NET 5 and click on the Create button as shown in the below image.
Once you click on the Create button, it will create the Console Application with the following structure. As you can see, it contains Program.cs class file and Dependencies Folder. Inside the Dependencies folder, it also has the Analyzers and Frameworks folder. At this moment the Frameworks folder contains only Microsoft.NETCore.App. Also, notice the project type as C#.
Now open the Program.cs class file and you should the following in it which includes the Main method which is the entry point to this console application.
Now if you run the application, then you will get the following output in the console window. That means our console application is working fine.
Now we will do some changes to this ASP.NET Core Console Application in order to make it an ASP.NET Core Web API Application.
Changing in CSPROJ file (Project file)
In order to make the console application a Web API Application, first of all, we need to make the following changed to the csproj file.
- Change the Project SDK to Web
- Remove the Project Output Type
- Verify the Target Framework Moniker (TFM)
In order to open the csproj file of this console application, we have two options either by double-clicking on the project or by right-clicking on the project and select the Edit Project file option as shown in the below image.
Once you open the Project file, you will get the following configuration inside the project file. As you can see in the below code, the Project SDK is Microsoft.NET.Sdk and the OutputType is Exe and the TargerFramework is net5.0.
So, we need to do some changes to the above CSPROJ file in order to make this project a Web API Project. As we are going to deal with ASP.NET Core Web Application, so we need to set the SDK type as Microsoft.NET.Sdk.Web. Again, as it is no longer a console application, so we need to remove the OutputType which is set to Exe from the Project file. Then we need to set the TargetFranmework. As already it is set to net5.0, which means it is dealing with .NET 5, so we dont require to do any changes. In case if it is target to some lower version of the .NET Framework, then change it to net5.0. So, with the above changes in place, your CSPROJ file should looks as shown below.
Once you save the changes, it will give you one warning that one or more projects have changes that cannot be automatically handled by the project system. Please save other changes and reload those projects to prevent data loss. Simply click on the Reload Projects button as shown in the below image.
Once you click on the Reload Projects button, the warning gone and you can also observe the project structure also has been changed. Now, the project type is changed to the web, it also adds Microsoft.AspNetCore.App inside the Frameworks folder which provides support for ASP.NET Core Web App as well as it also adds Properties folder with launchSettings.json file as shown in the below image.
The launchSettings.json file contains all the settings that are required to run this application in the Development environment. You will find the following settings in the launchSettings.json file.
These are the changes required in the project file. But this is not enough to run this application at this moment. We need to do some changes to the Program.cs file i.e. we need to add Web Host Builder. That we will discuss in our next Video. Here, in this Video, I try to explain the changes that are required in the csproj file to convert a console application to a web application.