ASP.NET Core Get Started

The learning journey continues with .NET Core stack, the latest framework from Microsoft. As a developer, I decided to check out ASP.NET Core.

The general path is to answer the question I asked (myself) in this post “How to take the advantages of Docker?” .NET Core seems to be a good candidate to explore.

As a habit, I immediately head to Pluralsight and check out the best Author, Scott Allen, course ASP.NET Core Fundamentals. Scott has a unique way of transferring complex stuff into your head. I highly recommend the course.

After watching the course, the biggest question I asked “What do I get? How do I start from here?” Follow step by step in the course is so simple. I will not learn much that way. Because I have experienced in code, not a starter.

How do I get the best out of the course?

Overview

I try to draw an image of the components that make up an ASP.NET Core application. Without an overall image, you will get lost in detail.

AspNetCoreComponent
ASP.NET Core Important Parts and Concepts

With a pencil, a paper, and my terrible drawing skill, I draw a picture of my understanding of ASP.NET Core pieces. Those are the fundamentals to get started with building web applications and exploring the power of ASP.NET Core.

Before explaining them, let take a look at the minimum code generated by VS2017

Empty Web project created by VS2017
Empty Web project created by VS2017

Very neat, clean! There are 2 files: Program.cs and Startup.cs.

Startup

The Startup is where developers build the application components. Take a look at my drawing, there are 3 major concepts

ConfigurationBuilder

In the past, we have web.config. We define connection string, appSettings, …

ConfigurationBuilder allows developers to define where/how to read the configuration information. Usually, they are stored in JSON file. What it means is that the configuration builder will allow developers to wire up the dependent configuration data from static files.

ConfigureService

For any application to run, we need services. This is place we set up our dependency injection (DI)

Note: You should be familiar with DI in practice.

ApplicationBuilder

The heart of the framework is the mighty ApplicationBuilder. As its name implied, it is a tool for us to build the application. It does many things. However, one of the important things is to build the Middlewares Stack.

How many middlewares do we have in the above code?

Answer: 2.

UseDeveloperExceptionPage: If there is an exception, display the exception page that developers can understand 🙂

Run: The middleware that handles all the request. It always returns “Hello Word!” text.

Middlewares are stacked. Which means that the order matters.

Some important behaviors of middlewares

  1. Order matters.
  2. One middleware can terminate the request which causes the next one is not called.
  3. There are many built-in middlewares, delivered by using NuGet packages.
  4. Most of them follow a naming convention. Usually, they start with UseXXX.

 

MVC Middleware

Let’s tweak the Run middleware a bit

Tweak the Run middleware
Tweak the Run middleware

In theory, we can build out web application by placing the logic inside the Run middleware. We have the power over the HttpContext. If we want to build our own MVC framework, we can do by following these high-level steps

If we want to build our own MVC framework, we can do by following these high-level steps

  1. Parse the request to extract information about Controller, Action, Parameters, …
  2. Dispatch the call to proper controller/action.
  3. Build HTML result from the controller/action result.

Or we can simply turn it on with

All the magic is at this line app.UseMvc() (from Microsoft.AspNetCore.Mvc NuGet package). We have to remove the Run middleware call. Otherwise, it will override the result produced in the MVC framework. Order matters, remember?

Recap

I try to draw a picture that I can understand; that should be easy to explain. From there, I explain my understanding of components/concepts that build up ASP.NET Core application. This, however, is not a “how to” post. Once you know what you want to do, you can search for “how to” quite easy. There are thousands of valuable resources out there.

By redesigning ASP.NET Core in this way, it is a powerful framework to play with. Developers have so many power over what they want to put into the application. We, as developers, know exactly what are in the pipeline. Grab your head around new concepts and take advantages of them.

I use this post as a reference for my understanding; and if asked, I can reference to this post. If you are reading this post, please give me feedbacks if I misunderstood something.

For anyone that is learning anything new, I suggest you draw a simple picture of your understanding. If you cannot, it is a sign of you have not understood it.

 

Leave a Reply

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

%d bloggers like this: