Posted on 8/12/2021 5:11:55 PM by Admin

Introduction to ASP.NET Core

In the previous article, I explained that ASP.NET Core is an application framework, which is used to develop Web Applications. As, ASP.NET Core targets the .NET Core/.NET 5+ Which are cross platform implantations of .NET , the ASP.NET Core is also cross-platform and runs equally well on macOS, Linux, and Windows. ASP.NET Core is a re-design of earlier Windows-only versions of ASP.NET. It was first released in 2016 and since then it has gain a huge popularity among web developers community.

The two ways of creating a web application

A web application can be of two types
    1. Single-page web application (SPA)
    2. Multi-page web application (MPA)

To understand the difference between SPA and MPA, think about the sections of a normal website. All website have some common sections like header, footer and sidebar and only the main section needs to be updated with user interaction as shown in below diagram.

Structure of a website
Structure of a website

Single Page Application

A single page application is one that never does a complete page reload, this means when you first visit the website then the server sends the main page to the browser and later with each user interaction only the parts of the web page changes, for example in a typical website, the header, footer and sidebar remains same, so there is no point to reload the whole page again and again for each new requests, so an asynchronous request is made by the browser to the server and only the related part of the current page changes.

Multi-page Application

A multi-page application is one that does a full page reload with each request, this means with each user interaction a complete page is sent to the browser from the server.

SPA vs MPA
SPA vs MPA

In short, a Single Page Application interacts with the user by dynamically rewriting the current page, rather than loading entire new pages from the server while a Multi-Page Application interacts with the user by completely replacing the current page by an entire new page from the server.

web development with ASP.NET Core
Web development with ASP.NET Core

In the above image Angular and React do not belong to the ASP.NET Core family, but are heavily used to create single page applications with ASP.NET Core API.

You would have already guessed it, that ASP.NET Core has 5 flavors or project types
    1. ASP.NET Core Razor Pages.
    2. ASP.NET Core MVC.
    3. ASP.NET Core Web API.
    4. Blazor WebAssembly.
    5. Blazor Server.

ASP.NET Core Web API : the odd man out

An API can only expose data. The only purpose of an API is to act as an interface between client and server, where the client can be anyone an angular app, a react app, a razor pages app, an MVC app, or a Blazor application.

If I ask you, what is the main characteristic of an Interface in general, then the answer will always come that it hides the complexity from the outside world and provide a means of communication with the internal mechanism, for example, a graphical user interface (GUI) hides all the complexity of internal implementations of software and provides us a way to communicate to that internals and yet we get the desired results. For example, Windows OS provides us menus, icons, toolbars, we just click on the icon and get the result, we don't care about the "How-to's", which means the internal mechanism is always hidden from us. This is the only purpose of an interface that is to hide the complexity and provide a way to access the functionalities.

But an Application programming interface (API) is slightly different from general interfaces like GUI or UI of an application. GUI or UI of an application are made for general or layman users, who know nothing about programming, but an API is always for programmers.

An API is for programmers by programmers

A C# programmer can create an API and a Java programmer can consume it in his application and vice-versa.

The ASP.NET Core Web API is an amazing platform or framework for creating Web APIs where Web API means an API that can be accessed through the internet (web). you can think of Web API as a set of classes, and methods that act as an interface for a user where users mostly are Web developers or software engineers, and they make use of it in their application to solving something. For example, I can use the Twitter API in order to post my tweets directly from my application.

Now, it's time to make you all familiar with another term "Services". When we say, we have created a web API or we are going to create a web API, it means we are saying that we are going to provide to you an Interface to access a Service through the web.

In the physical world, a service is something that solves one and only one type of problem but it solves it completely. For example, a Postal Service can be used to send letters and parcels from one place to another but it can not be used to buy or sell products.

From a programmer's perspective, a service is a self-contained independent functionality. To understand it better let's take the example of Google. Google provides different services like Gmail, Map, News, YouTube, calendar, etc. If you can access any of these services in your application it would mean that Google has an API for that particular service, In other words, Google is providing you an interface to interact with that particular service. For example, Google has YouTube API means Google is providing us an interface (classes and methods (endpoints)) to interact with YouTube service.

A service can be either Soap based or Restful. With ASP.NET Core web API we create Restful services. I have already discussed the Restful services here.More discussion on this will be on the ASP.NET Core Web API section.

Multi-Page Application : What to choose ASP.NET Core Razor pages or ASP.NET Core MVC?

As per Microsoft recommendation, we should use the new ASP.NET Core Razor Pages to create Web UI and the ASP.NET Core MVC should only be used to create web APIs. Yes, we can create a web API (Restful services) using either of the project types i.e ASP.NET Core web API or ASP.NET Core MVC, both project templates follow the MVC architectural pattern. In short, ASP.NET Core provides a unified story for building web APIs and web UI.

Razor Pages is a page-based programming model that makes building a web UI easier and more productive so Microsoft recommends using Razor pages over MVC (controller with views), however, it is completely up to your personal preference and your organization's project development architecture and patterns.

Single page application : ASP.NET Core Blazor

JavaScript frameworks like Angular and React have gained huge popularity in recent years, it would not be wrong to say that these client-side SPA frameworks were dominating the server-side frameworks like PHP and ASP.NET. For Microsoft, to retain their reputation and position in the web development market, they definitely needed to do something and this is the reason Blazor has been launched.

For us, who are connected and using Microsoft technologies for a long period of time, it is really graceful that Microsoft thought about it.

Why to choose Blazor over React/Angular

Frankly speaking, you don't need to go for Blazor if you are already good at JavaScript or typescript. But, if you are like me who is in love with C# and find yourself more confident in c# than JavaScript then Blazor is for you.

With Blazor, we can create equally rich interactive UIs as we would with Angular or React but with c# as the language instead of typescript or JavaScript. With Blazor, Microsoft made Client-Side web development possible. NET. In other words, Blazor simply means Client Side web development with .NET which offers the following advantages.
  • we can write code in C# instead of JavaScript.
  • we can Leverage the existing .NET ecosystem of .NET libraries.
  • we can Share app logic across server and client.
  • We can take the benefit from .NET's performance, reliability, and security.
  • we can use our favorite IDE, Visual Studio on Windows, Linux, and macOS for the App development.

Now, you might be thinking how does Browser execute the C# code? and the answer is WebAssembly. WebAssembly is based on open web standards and is a native part of all modern browsers including mobile browsers. With WebAssembly, we can run any type of code in the browser. This means for the blazor application to work, we don't need to install any special plug-ins on the Client machine.

Application development with ASP.NET Core

We can use the famous and widely accepted n-tier architecture to create an application using ASP.NET Core, however, you are obviously not bound to any architecture, and if your web Application is really very small and simple then there is no need to create separate layers or to follow any architecture.

Following an architecture like n-tier, increases the reusability of code, you can use the same Business Logic DLL or Data Access DLL with other projects. It also makes it simple to maintain a project. For example, if the Data Access codes were written using the Entity Framework ORM and now you want to use a different ORM or want to point different database altogether then you can easily do so by writing another implementation of the Data Access without affecting other layers of the project, Obviously, even if you use n-tier, it would not be possible to switch between implementations if you haven't followed the Dependency inversion principle correctly.

App development with ASP.NET Core
Application development with ASP.NET Core

As you can see, that among the 5 ASP.NET Core project types, the 4 project types that is Razor pages, MVC, Web API and Blazor Server can directly use the DLL (Class Library Project) just by adding the reference but the Blazor WebAssembly that is also known as Blazor Client can't reference the DLL directly, it will first call the Web API using HttpClient and the web API will provide him the necessary data. Obviously, the web API can be used with Other all project types but the point I am trying to make is Blazor WebAssembly App completely runs on the client machine (Browser) , while the Blazor server app is executed on the server from within an ASP.NET Core app and UI updates, event handling, and JavaScript calls are handled over a SignalR connection.

Key Features of ASP.NET Core

1. ASP.NET Core is a complete redesign of ASP.NET 4.x, there is a huge architectural change between ASP.NET 4.x and ASP.NET Core.
2. ASP.NET Core is modular, the request processing pipeline is made using middleware, where middleware is like HTTP Modules of ASP.NET framework, each middleware serves a specific purpose. This means, we only use the modules that we want that make ASP.NET Core lightweight.
3. ASP.NET Core is Cross-platform, which means we can develop and run ASP.NET Core applications on Windows, macOS, and Linux.
4. ASP.NET Core is open source and community-focused, the source code is available at Github, that ensures quick bug fixes and releases.
5. ASP.NET Core support for hosting Remote Procedure Call (RPC) services using gRPC.
6. ASP.NET Core provides a cloud-ready, environment-based configuration system, which we will discuss in the next article.
7. ASP.NET Core provides built-in DI (Dependency injection) support.
8. ASP.NET Core supports side-by-side versioning, this means an installation of different versions of the .NET runtime on the same machine is possible this allows multiple services on the same server, each of them on its own version of .NET Core/5+. It also lowers risks and saves money in application upgrades and IT operations.