Asp.Net Core Overview

Spending the past year on building and supporting Unison.School gave me an in-depth, full-stack experience in developing web apps with Asp.Net Core. In this post, I will describe the basic components and aspects of Asp.Net Core, and give my personal opinions on the software.

PowerPoint Presentation

Development

Runtime

Asp.Net Core is an implementation of .NET Core, which runs on the CoreCLR runtime, which is a virtual machine that executes .NET code. Unlike the .NET Framework, which is Windows-specific, .NET Core is designed to be able to run on multiple platforms, including Windows, MacOS, and Linux.

Asp.Net Core is designed to make Web Apps and Wep APIs which are deployable on any system above, as well as in Azure Web Apps or Docker containers. It is built on the decades of experience with Asp.Net and Asp. In addition to adding cross-platform capabilities, Asp.Net Core introduces built-in Dependency Injection and support for Razor Pages. Like Asp.Net, you still have access to traditional routing and model binding features.

.NET Standard

One goal that Microsoft has pushed toward while releasing .NET Core, is to also increase the re-usability of .NET code across multiple projects. With .NET Standard, they have succeeded. .NET Standard is a compatibility requirement or specification that the most recent versions of .NET Core, .NET Framework, Mono, and Xamarin all meet.

This specification means that you can write .NET Standard libraries, and access them from any modern .NET project. When creating an Asp.Net Core web app, it gives you the ability to share large amounts of code with future mobile or desktop apps.

Visual Studio

Any experienced .NET developer is familiar with Visual Studio. VS 2017 supports Asp.Net Core, and includes multiple starter templates to help you build a web app or API.

Visual Studio Template Options

VS also supports graphical interfaces for Nuget, git, and other extensions.

Command Line

For those interested in a quick clean start, or using a different editor than Visual Studio, there are also simple .NET Core commands, such as dotnet new, dotnet build, and dotnet run.

Dependency Injection

DI Setup in Startup.cs

I love the magic of Asp.Net Core’s Dependency Injection (DI). While there are many third-party DI frameworks that probably work just as well, it is nice to offer this as a built-in feature, to encourage good code architecture. I have worked on projects without DI, and while it works, the factories to instantiate objects can be verbose and confusing at times.

Templates and Applications

As stated above, the two main uses for Asp.Net Core are for Web Apps and Web APIs.

Web APIs

The API template gives you a very simple project with Controllers that listen for web calls and route them to specific actions.

Controller

This simple interface can then be connected to any business classes, databases, or other services.

MVC Web Apps

The MVC (Model, View, Controller) web app template builds upon the Controller foundation from the API template, and adds Models (aka Data-Transfer Objects, View Models), and Razor Views, which are HTML pages with embedded C# code, that is converted into HTML on render. Each Controller can return one or more views as an Action Result.

View Model (Data Transfer Object)
Razor View

The nice thing about the MVC approach is the very clear separation of concerns between the Controllers and the Views. However, this can also be potentially more structure than is needed, especially for web sites that do not also include API services.

Razor Pages

A more recent model for Asp.Net Core is that of Razor Pages. While Razor Pages use the same cshtml structure as Razor Views, they also each come with a PageModel c# file, which is more akin to MVVM designs or XAML code-behind classes.

PageModel
Razor Page

Notice that the only difference in the Razor file between a View and a Page is the tag @page at the top.

The Razor Page PageModel is great for organizing all the relevant code for a specific page in one place. However, unlike a Controller class, it is not ideal for handling multiple various calls, such as AJAX and API requests.

 

Front-End Frameworks

Asp.Net Core is completely compatible with any front-end (client-side) web frameworks. As you can see in the templates image above, starter templates exist for using Asp.Net Core with both Angular and React. Templates for other frameworks can be found online.

Blazor

Blazor is an exciting new development for the .NET community. Based on the new WebAssembly (WASM) standard, frameworks can now be written to compile code in any language to be run in the browser! The Mono development team has taken on this challenge, creating a .NET runtime that runs in WASM and on the browser.

Blazor is designed to take the existing Razor syntax, and use it via Mono/Wasm to replace other front-end javascript frameworks. While exciting, this project is still in it’s infancy, and not ready for production apps.

 

Extensions

As .NET Standard compliant, Asp.Net Core opens us up to a huge variety of Nuget packages that are compatible with our web apps.

Entity Framework Core

Entity Framework is a .NET Object-Relational Mapping (ORM) framework that wraps SQL Server or other databases in a level of abstraction, allowing C# writers to model, query, and save to the database without leaving the familiar .NET language. EF Core is re-written specifically to work with the .NET Core framework, and it can be included in new projects with a click of a button, or imported from Nuget.

EF Core Database Context

Signal-R

While I haven’t had a chance to explore this yet, the real-time web communication library Signal-R has recently been given an Asp.Net Core library.

 

Deployment

Asp.Net Core apps can be deployed on any Windows Server via IIS, just like traditional Asp.Net. However, as a cross-platform solution, they may also be deployed on a Linux server via Apache or Nginx. On Azure, you can use an App Service instance to upload your app as a managed service, without the need to deal with the server at all!

Azure App Service Portal

Azure also gives you tons of tools for managing your app, including Application Insights, auto-scaling, and site and database backups.

Leave a Reply

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