We use cookies to provide you with a great user experience, analyze traffic and serve targeted promotions.   Learn More   Accept
Card image cap

Getting Started with ASP.NET Core - A Jumpstart Guide for Beginners

ASP.NET Core Tutorials  • Posted 6 months ago

So far it has been an exciting journey sharing tips and tricks I have seen in my own experience working with ASP.NET Core which has been my primary development technology since over an year. While the feedback has been mostly overwhelming, I wasn't sure if my pieces of experience through my articles have been in a sequence - which can be quite helpful for beginners who can be a lot benefited to jumpstart on development in dotnet core. And then I got an idea to place all my articles, which are otherwise independent of each other talking about a single section or a single scenario using a particular feature, together along with some filling content which can then create a catalog of a complete asp.net core jumpstart tutorial helpful for anyone who's just getting started on this journey like I did an year ago. Also, I felt this can be quite helpful for other readers as well who have been referring to these individual pieces of write-ups to have a complete picture of my experience so that it can be quite helpful in their journeys as well. And so let's get started on my complete picture on "getting started with asp.net core" which is also "a jumpstart guide for beginners."

Setting up the Context:

While there has been the dotnet framework prevalent for over a decade or two helping developers build awesome applications over the windows platform along side the other alternatives over the market such as the Linux platform or the Mac OS platform which are the major players in the Operating Systems. While the other development frameworks such as Java, PHP, Python or Ruby which are quite popular for their own features and advantages, the dotnet framework platform has become quite old and underwhelming for its limitations on platform dependency and since the age of mobile optimized web solutions has dawned, it has become even tough to match up with the high performance and faster load times adding to the woes. This paved way for the idea of a completely new, cross platform, high performing, light weight development framework written in dotnet which can help developers achieve better solutions for problems spanning across not just the web, but across newer and emerging platforms such as IoT and Cloud based solutions, apart from the traditional desktop based solutions. This was the beginning for the dotnet core foundation.

What is dotnet core ?

The dotnet core, as it is called, it an open source, cross platform and light weight development framework built from the scratch using the experience on the traditional dotnet framework which can help developers solve problems on multiple environments such as web, desktop, Iot and Cloud while not bothering about the environment the application should run; whether its windows, linux or mac or beit any operating system. The solutions developed on the dotnet core come up with a "write once run anywhere" feature, meaning once the code is built we can decide and compile the code into executables for whatever runtime we chose to be. The three major characteristics for dotnet core which makes it a developer's favorite is:

  1. Lightweight: The dotnet core runtime is designed to go smooth on the processors and memory loads so that all it takes is a very limited chunk of size on the memory for its execution; which is ideal for running on lighter environments such as IoT devices.

  2. Performance: The platform is built from the scratch taking in some of the best practices across all platforms in the market as well as in the dotnet world. What is the end result is a rather high performing ecosystem that is designed to take in any level of request loads and still go high in performance; which has been something a bit lacking in the legacy dotnet framework.

  3. Open sourced: One of the biggest advantage of dotnet core is its open-sourced nature; probably one of the first dotnet ecosystem to be so. Since its backed by the open-source community of thousands of developers; updates and fixes go blazingly fast and has much scope for a better evolution, which is a welcome step these days.

Components and Features - The Jumpstart Guide:

The dotnet core framework is quite different from the earlier versions of dotnet framework, starting from its design till the way how it works. Let's break the things into three categories basing on the aspects of their usage. We go by components which are used at:

  1. Development
  2. Testing and
  3. Deployment

Development:

Let's first understand how a typical aspnetcore application looks like. There are three major components we should look at :

First there is the Program.cs file which is the starting point of the application. This contains the Main() method, which is similar to any console application is where the application boots up. This is to facilitate the self-hosting nature of the dotnet core application. Here we declare a HostingBuilder (or a HostBuilder since dotnetcore3.x) which creates the environment and all things needed for the application to go up and running.


namespace dotnetcore2xapp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
}


namespace dotnetcore3xapp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

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

Second, there is the Startup class which is used to build the service pipeline and application request pipeline. A service pipeline is where we declare and define each and every service which shall be injected into the application logic during the development and be used according to their scopes.

Getting Started with The Startup Class

The ecosystem comes with an IoC (Inversion of Control) container that takes care of the object initializations and memory managements for us; making developments a bit easier. These objects are defined in terms of services, which are later injected into the requiring classes through constructors. This is known as Dependency Injection. Generally, there are three kinds of service lifetimes defined in any DI pattern containers which relate to how these objects are maintained in the memory and how they are served during the requests.

Getting Started with The Container Services and their Lifetimes

The application request pipeline is the path a typical HTTP request passes through before reaching its endpoint for execution into a response. During this path, we add multitudes of individual component block which either validate or enhance the incoming request as per our expectations. These individual components which run perpendicular to the request path are called as middlewares. A middleware has the capability to read through the request and decide whether it can go ahead or needs to be turned down. In aspnetcore, almost all the features internally run in the form of middlewares which can help in a better request assessment and application design.

Getting Started with The Middlewares

Third, there is the appsettings.json file which is now the design for a configuration file (in contrast to the web.config). The appsettings json file holds all the settings required for the application in the form of key value pairs which are available to the application right from the Startup class constructor. These settings are wrapped by the default IConfiguration instance which is injected from the Startup and can be used for various condition based logics or instantiations; till the induction of variable setting values. We can further enhance this by means of a template based Configuration class which can then provide a strongly typed configuration for our use.

Getting Started with The AppSettings and Configuration

The database connectivity to an aspnetcore application is provided in the form of a dbcontext, which is again a service inducted into the container. This dbservice makes use of the high performing EntityFramework Core, which is a light weight version of the original EF of the dotnet framework. This makes database logic operations easier and much more elegant. The dbcontext class internally makes use of the UnitOfWork pattern for its instance management and its transient in nature. We can make use of the migrations to setup and seed database if not add new entities into the database model. It maintains a database-model-to-database mappings for tracking any slight change in the db model and update the same to the actual db.

Getting Started with The DbContext and EFCore

The dotnet core also provides a solution for handling individual files and asset content on the server when need to be served to the user or be included into a web page; such as style sheets or script files. This is handled by means of a StaticFile middleware which helps not only maintain and differentiate these asset file paths from the actual request paths but also help in better management of these assets on how they are persisted in the client.

Getting Started with The Asset Management and Static Files

To Get Started on building an aspnetcore application of any mode; such as an MVC application, a WebAPI or a Server application with Angular Frontend and so on, we have the dotnetcore CLI; a command-line tool which works best for us to get on running with a boilerplate code stuffed with all basic things done. This helps not only get started faster, but also helps avoiding the initial hiccups for creating and setting up a new project file system.

Getting Started with The dotnet core CLI

Testing:

dotnet core provides a great support for unit testing by means of its features loaded dependency injection container which helps us keep up with the Testable project components along with our development. This coupled with another great open-source unit testing framework called xUnit, makes our programming lives easier. Also, the dotnetcore CLI has support to create an xUnit test project boilerplate code ready for us, so that we can go ahead and build the things for testing. A typical unit testing or an integration testing goes by the analogy 3As, which are to Arrange, Act and Assert the tests for their functionality. While unit tests run at a granular level of components, grouping two or more components together while testing makes it an integration tests.

Getting Started with The Unit Testing

Getting Started with The Integration Testing

Deployment:

The ecosystem is designed to run in any webserver, be it IIS or any other and also as a self hosted service. This is enabled by the fact that dotnet core comes in with a built-in web server called Kestrel which does the request handling job without having to bother about any other web server to be used. And when we require this setup to run in any preexisting web servers such as IIS or any other; we have integrations by means of added methods which do the job for us.

Getting Started with App Deployment

These are the most important concepts a beginner should keep in mind, which can help for a quick head start into the ASP.NET Core development. While its been into dotnetcore3.x by this time, the above concepts still feel relevant to any version of aspnetcore development starting from dotnetcore 2.x

Jumpstart Guide - In Order:

Development:

Getting Started with The Startup Class

Getting Started with The Container Services and their Lifetimes

Getting Started with The Middlewares

Getting Started with The AppSettings and Configuration

Getting Started with The DbContext and EFCore

Getting Started with The Asset Management and Static Files

Getting Started with The dotnet core CLI

Testing:

Getting Started with The Unit Testing

Getting Started with The Integration Testing

Deployment:

Getting Started with App Deployment