What is .Net Core ?
.Net core is a free open source framework developed and maintained by Microsoft. This framework helps us to build and run our applications in all the platforms such as macOS, Windows and Linux operating systems unlike .Net.
Difference between asp.net & asp.net core ?
- To build and run our application we need dotnet software development kit, that can be downloaded from https://dotnet.microsoft.com/ for the respective OSs. Download the LTS
- Once the download is complete, check if it has been downloaded properly by running the following command in the command-line “dotnet –info”, the output will be all the information of the SDKs and the run times that have been downloaded in your system.
- Recommended IDE to be used will be Visual Studio code since it’s free , available for both Windows and Mac OS and open source , though windows users might prefer Visual Studio which is also fine.
- Postman can be used to test the APIs that we have developed
Creating a dot net core project using dotnet CLI
- First create a folder with the name of the project let’s name it as OurNewDotnetProject.
- In order to create a dotnet core project we need to first create the solution file.
- A solution file maintains the structure of the project that will be used by the IDEs. When we try to open the project, the IDEs use these solution files to get information about the hierarchy in which the files are stored in the project while the data in the files are not stored. For more information on the solution files please refer to the following document https://docs.microsoft.com/en-us/visualstudio/extensibility/internals/solution-dot-sln-file?view=vs-2019
- To create the solution CD into the project that has been created and type the following command “dotnet new sln”
- You’ll be prompted with The template “Solution File” was created successfully this message when the solution file has been created successfully.
- Another important command to be noted will be “dotnet new -h”, this provides the list of templates and functionalities available in the SDK version. The output of the command will be like the following
- We are now going to use the web api as the project template that we are going to create.
- Use the following command to create a web-api inside your project directory “dotnet new webapi -o MyAPI”
- -o is to create a separate folder for our API project
- Now if we list the contents of the OurNewDotnetProject project folder, there must be two things in it , the solution file and the API project.
- Our next step will be to add our API to the solution file, the following command will do that “dotnet sln add MyAPI” this will be the success message “Project `API/API.csproj` added to the solution.”
Contents added as part of the API Project
When the code is opened in the preferred IDE, some files would have been already created for you along with the solution file, which will look like the following,
One of them is Program.cs.
- In this file we have a Main method which will be the entry point when we run our application.
- This main method will have the following implementation by default.
public static void Main(string args)
- This calls a method called create host builder, this method will initialise the Hostbuilder class behind the scenes with following pre configured defaults and returns the same
- If we check the implementation of CreateHost Builder
public static IHostBuilder CreateHostBuilder(string args) =>
- We have a method called ConfigureWebHostDefaults method, called inside this method. Now what this method does is, it initialises a new instance of IWebHostBuilder class with pre configured defaults and returns IHostBuilder, this is where the following things happen.
- Sets the content root path of the project , by default this path will be the current Working directory
- It will load the configuration which can be from many sources such as (in our case it is appsettings.Development.json)
- DOTNET_ prefixed environment variables
- Command line arguments
- Configure the ILoggerFactory to log the console and debug output
- Use kestrel as the web server
- The IWebHost builder is initialised using the startup class which we will see in the next section.