ASP.NET Core Overview

ASP.NET Core (previously ASP.NET 5) is a new open-source and cross-platform framework for building modern cloud-based Web applications using .NET. This is completely re-done new framework designed for building modern web applications. What is this new ASP.NET Core framework all about?

framework

Image Source: build.microsoft.com/sessions
 
Everything that is done with ASP.NET core is with nuget packages. You can pull as much as you want into your application. Requests of  handling pipeline hasbeen much streamlined. ASP.NET core has been designed with cloud in mind and it is optimized for cloud-based scenarios. There are great command line tools that supports on various platforms (windows, mac and Linux). There is a lot of interest in the community for the support. It is really fast now!! The simple hello world application handling 3.5 million requests per second!!!! Don’t expect your heavy-weight applications to perform the same.The resources for learning more about these features can be found here. The documents can be found here and sample code can be found here.

ASP.NET Core in a Nutshell

Nutshell
Image Source: build.microsoft.com/sessions
 
.NET Framework still stays there and it is windows component, on top of this you see ASP.NET stack with system.web dll on top of this you see different frameworks web forms, MVC and Web API.

In above picture you can also notice there is a new framework .NET Core which is based on core CLR and it is open source and runs cross-platforms Mac, Windows and Linux. In the new world MVC also changing and it is called ASP.NET Core MVC.

ASP.NET Core features

You will get a new hosting model, you will get a web server with code name Kestrel. You will also have the option to define how your startup looks, in other words request pipeline. It also has the Middleware which is asynchronous and incredibly light-weight. There are a bunch of middlewares that comes as outofbox like for routing, authentication, static files, diagnostics, error handling, session, CORS , localization and custom. Of course you can write your own middleware too. Dependency Injection is built-in in ASP.NET Core. You will get a new configuration system where you don’t have web.config instead you will get a light-weight configuration model which is based on provider, which you can read from variety sources json files, xml files and environment variables. What you can develop with ASP.NET Core is MVC, Identity, SignalR (future).

ASP.NET Core MVC features

To create the new ASP.NET Core MVC project template, install Visual Studio 2015 update2 from here. Also install ASP.NET 5 RC1 from here.

web app

Create a default web application by selecting the template shown in the above picture. This template has some pre-configured features for you, it’s got identity, client-side technology like gulp, bower and npm etc.all these features will get installed with packages. Solution structure looks familiar to the ASP.NET MVC.

code

What is new in ASP.NET Core project solution explorer? One is project.json file. As you know ASP.NET Core is built on .NET Core, what makes your project is .NET core is project.json file. IT defines all package dependencies for your application. You can also specify your project to compile on .NET Framework and .NET Core Framework in project.json file.

json

The Startup class defines all ASP.NET Core features and the file looks as below,

features

There is a main method as starting point for the application, in .net core world asp.net projects are really like console applications. In this main method you hookup hosting method to run the application. You can run the application using console which is self-hosted or you can run on your iis.

iis

There is a constructor in startup class which sets some initialization, for example, using Configuration Builder you can set different configuration providers, this template file is configured to use a json file which contains all configuration related keys,

code

The appsettings json file looks as below. You can also add environment specific configuration files using this file. AppSecrets now no longer needs to be stored in configuration file, they are storing somewhere in user profile, runtime picks them and uses them during application startup.

code

Request pipeline setup has actually happened at configure method to handle request in your application.

code

First line sets up the debugging logging or console logging. You can also specify what sets of middleware you want to use in development mode to get the nice productive development experience. If you are not running in development mode then you can add a nice exception handler middleware to redirect to a error page. Notice there is an integration with iis and final piece of code in startup tells to use mvc. MVC is also routing middleware. UseMvc is actually calling mvc router and eventually calling mvc handler.You can also specify inline constraints in router configuration. Routing system also fall-backs to the next route when you have multiple routes and one of them fails to handle the request.

There is a ConfigureServices method where you can configure all your services like Dependency Injection, Entity Framework.

code

You can even add application related services account confirmation or email sender etc. These services are available throughout your application, for example if you write custom service then you can inject them into a controller. If you open Account Controller there you will find all services that you are injecting,

code

You can also inject services into your views, for example if you open login page view you can find a new razor directive @inject, first parameter is a type of service that is made available forthe page and the second parameter is the new property that you want defined for your page to access that service,

code

you can use this service with that property later in the page as shown below,

code

you can inject services in views that are more view specific for example IView localizer service. Another important feature is razor pages now load asynchronously,

code

There is a _ViewImports cshtml which is a hierarchical file where you put folder hierarchy so every time a view being rendered it search up for folder hierarchy for viewimports file. It has set of directives that commonly used in views. Remember we used to do this in web.config in old world! In other words it is a namespace declaration file.

code

You can also include taghelpers in this viewimports file. Tag Helper is a way to associate a small piece of server side rendering logic with a tag.In the above screen addTagHelper is adding all default mvc tag helpers to your application. This is an optional feature.

Adding or Customizing html helpers in old mvc projects is a little difficult, for example, adding an attribute.In ASP.NET MVC core world it is simplified , the old and new cshtml can be compared as shown in the following screenshot,

code

Overall the new ASP.NET Core gives you unified programming model for developing MVC, Web UIs and Web API applications.
 
Read more articles on ASP.NET Core: