ASP.Net Core Tutorial for Beginners

  • by

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 ?

Development Kit

  1. 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 
  2. 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.
  3. 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.
  4. Postman can be used to test the APIs that we have developed

Creating a dot net core project using dotnet CLI

  1. First create a folder with the name of the project let’s name it as OurNewDotnetProject.
  2. In order to create a dotnet core project we need to first create the solution file. 
    1. 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
  3. To create the solution CD into the project that has been created and type the following command “dotnet new sln”
  4. You’ll be prompted with The template “Solution File” was created successfully this message when the solution file has been created successfully.
  5. 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
  1. We are now going to use the web api as the project template that we are going to create.
  2. Use the following command to create a web-api inside your project directory “dotnet new webapi -o MyAPI”
    1. -o is to create a separate folder for our API project
  3. 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.
  4. 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. 

Program.cs

  1. In this file we have a Main method which will be the entry point when we run our application.
  2. This main method will have the following implementation by default.

public static void Main(string[] args)

       {

           CreateHostBuilder(args).Build().Run();

       }

  1. 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
  1. If we check the implementation of CreateHost Builder

public static IHostBuilder CreateHostBuilder(string[] args) =>

           Host.CreateDefaultBuilder(args)

               .ConfigureWebHostDefaults(webBuilder =>

               {

                   webBuilder.UseStartup<Startup>();

               });

  1. 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.
    1. Sets the content root path of the project , by default this path will be the current Working directory
    2. It will load the configuration which can be from many sources such as (in our case it is appsettings.Development.json)
      1. DOTNET_ prefixed environment variables
      2. Command line arguments
      3. Appsettings.json
    3. Configure the ILoggerFactory to log the console and debug output
    4. Use kestrel as the web server
  2. The IWebHost builder is initialised using the startup class which we will see in the next section.

Leave a Reply

Your email address will not be published. Required fields are marked *