ASP.NET Core 1.0 a.k.a ASP.NET 5 - Conceptual Overview
Posted by: Ravi Kiran
in Category ASP.NET
Abstract: ASP.NET Core is a redesign of ASP.NET. We will look at the ASP.NET Core platform architecture and some of the most essential ASP.NET Core components.
ASP.NET 5 has been renamed to ASP.NET Core 1.0. All references to ASP.NET 5 in this article should be read as ASP.NET Core.
ASP.NET Core 1.0 is a significant redesign of ASP.NET. Unlike other versions of the technology, this version is not an enhancement to the previous version. It is written from scratch to make the technology better and lighter.
You may have a question as to what made Microsoft take such a bold step and rethink about everything that they built over the years and start afresh. The reason is, the new platform competes with other web platforms like Node.js and Ruby and this competition makes it necessary to make the platform lighter and crop existing platform as they stand today.
In this tutorial, we will take a conceptual overview of ASP.NET Core a.k.a ASP.NET 5, take a look at the ASP.NET Core platform architecture and will understand some of the most essential ASP.NET Core components.
This article is published from the DNC Magazine for .NET Developers and Architects. Download this magazine from here [Zip PDF] or Subscribe to this magazine for FREE and download all previous and current editions
ASP.NET Core - What's in it?
ASP.NET Core is created with modularity and performance in mind and to make the technology available on as many platforms as possible. Dependency on System.Web.dll has been removed for this purpose. The System.Web.dll works only on Windows platform taking advantage of certain features that only Windows can offer, so there was no way it could be made cross platform. The removal of System.Web.dll has made ASP.NET Web Forms distant from ASP.NET Core.
Editorial Note: Microsoft continues to invest in Web Forms including adding support for Roslyn. Some additional features added to Web Forms is HTTP 2 support and async model binding. To learn more, please refer to a fantastic article by Rion Williams on What’s New in ASP.NET Web Forms in .NET 4.6 https://www.dotnetcurry.com/aspnet/1127/aspnet-webforms-new-features
In ASP.NET 4.5, ASP.NET MVC and Web API used to work in a similar fashion, but they were built on completely different set of class hierarchies under the covers. ASP.NET Core converges these two components together and provides a unified way of creating both views and APIs. Views and objects are just two different return types of the base Controller class. As we have the same controller handling both types of requests, we can use the same set of action filters and same routing for both of the response types.
Supported .NET Frameworks
In case you are not aware, Microsoft created a new light weight and cross platform version of the .NET framework. It is called .NET core. The .NET core includes a subset of class libraries from the full .NET framework and is designed to work on multiple platforms. It has a corresponding runtime called CoreCLR. This .NET core does not contain libraries like System.Drawing, System.Windows and other libraries that are dependent on the Windows platform to achieve their functionality.
ASP.NET Core is designed to work on both the full .NET version, as well as on the .NET core version. The code that is targeted to run on .NET core can be executed on any platform. ASP.NET 5 runs on Roslyn, so the builds are completely dynamic. That means in order to see the updated result after making changes to any C# file in the project, we don’t need to build the project. We can simply refresh the browser to see the updated result.
Figure 1: Core components of ASP.NET Core
How does an ASP.NET Core 1.0 application work?
When an ASP.NET Core application starts, it runs only the essential components that are needed to start the server and serve simple pages. Unlike previous versions of ASP.NET, it doesn’t start a set of features that the application may or may not use. If we need a feature, we need to invoke the respective middleware. MVC, Entity Framework, Identity, Caching, Logging and any services that we need to use in lifetime of the application have to be loaded as middleware services.
ASP.NET Core contains a built-in IoC container. It can be used to configure dependencies for the application. It is possible to replace it with an IoC container of your choice.
The Solution explorer window of a new ASP.NET Core roject created on Visual Studio 2015 looks like figure 2:
Figure 2: ASP.NET Core Project Structure
It has the following things at its core:
- · project.json: This file replaces Web.config and package.config files from previous versions. The JSON schema of this file is stored at schemastore.org. Following are some of the important configurations provided in this file:
version: version of ASP.NET Core to use
compilationOptions: These are passed to Roslyn for compiling the code. It specifies the version of C# to target, whether to allow unsafe code and a few other options
commands: These are the commands that can be executed on a PowerShell command prompt on Windows and on terminals of OSX/Linux if DNX (Dotnet Execution Environment) is installed. In the project.json file created by the template on Visual Studio, you will find the command web assigned with Microsoft.AspNet.Server.Kestrel. Kestrel is a cross platform web server to run ASP.NET applications from command line and it helps us in running ASP.NET 5 apps on Linux and OSX
frameworks: It is the list of frameworks on which the application can run. You can specify multiple versions and use one of them depending on the platform on which the application runs
dependencies: It is the list of NuGet packages with their versions and the list of class libraries that the project needs to run. If you are using Visual Studio, the dependencies are loaded as soon as you add a dependency and save the file. The list of installed dependencies can be found under references section of the application. If you edited this section using other editors on Windows or on other platforms, then you need to run the command “dnu restore” to get the dependencies installed. DNU (Dotnet Execution Environment Utility) is the tool that helps in dealing with packages
scripts: These are the tasks to be executed when the application is built or published. It is used to install front-end dependencies using NPM, bower or other package managers, run tasks using grunt or gulp in order to make the things ready when the application runs
- Startup.cs: It is the starting point of the ASP.NET Core application. If you are familiar with Katana project, this file is inspired from the same project. Name of this file is by convention. It has a static void main method and as you would have already guessed, this method is the starting point of the application. The methods in this class are used to load middleware services, configure abstract and concrete types for IoC container, to define routes and to configure any service that is loaded as a middleware. The methods Configure and ConfigureServices defined
- global.json: This file is present at root of the application folder and it contains solution level settings. By default, it contains names of the projects and SDK version. These settings are used for all projects under the solution.
- appsettings.json: This file is used to store application settings. It may include things like database connection string, mail server to use while sending mails, folder path where uploaded files have to be saved and any such settings that are used by the application. Name of this file is not by convention. It is read in the Startup.cs file. We can store environment specific application settings in individual files and load one of them according to the current environment.
- gulpfile.js: This file defines a set of gulp tasks to copy, combine, minify, uglify and clean the front-end files and dependencies. The tasks can be executed via command line or, can be configured to run during build or publish in the project.json file. The project.json file created during
- package.json, bower.json: By default, these files are not visible in the solution explorer, we need to select the view all files option to see these files. The package.json file contains list of NPM dependencies and the bower.json file contains list of bower dependencies to be used by the project. The installed dependencies can be seen under the dependencies section of the solution explorer
ASP.NET Core Request LifeCycle
Now that we know the different pieces of an ASP.NET Core application, let’s now take a sneak peek of what happens when a URL of an ASP.NET Core application is invoked. Figure 3 shows what goes on after the request reaches the web server:
Figure 3: ASP.NET Request Life Cycle
ASP.NET Core is created from scratch to make the platform better and to embrace the Node.js ecosystem inside Microsoft’s tools. This change makes the developers working on ASP.NET stay abreast with the latest open source tools and it opens up the opportunities to write and run ASP.NET code on multiple platforms. Yeoman has a few generators to scaffold ASP.NET Core applications, they can be used on non-windows platforms to quickly start writing an application. I hope this article gives you a good introduction to concepts of ASP.NET Core. We will have more articles on this topic in future.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce the The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core. Use these concepts to deepen your existing knowledge of C# and .NET, to learn new features or to crack your next .NET Interview.
Click here to Purchase this eBook at a Discounted Price!