ASP.NET Core - the newest framework for cross-platform web development. While his popularity (as well as the number of vacancies) is just beginning to gain momentum it's time to learn more about him. But to ensure that all knowledge does not evaporate immediately after reading - add an essential practical part. Create a simple application to test read.
If you think that you are already quite cool in the new framework, you can try to pass the test before you read the article.
Link The entire project code can be viewed on the
githaba .
The first part includes:
')
- What are .NET Core and ASP.NET Core?
- Basics of creating an application and its structure
- Adding new items, scaffolding
- Basics of Embedded Dependency Injection
- Deployment in Azure

We will understand in terms. One of the most misunderstood points is the relationship between the old ASP.NET MVC framework and the new ASP.NET Core, as well as the differences between .NET and .NET Core. Let's start with the last one. .NET Core is a common software development platform. In fact, this is another implementation of the .NET standard (other implementations are .NET, Mono). The differences and features of this implementation (
.NET Core ) are that it:
- Open source
- Cross Platform
- Flexible to install - can be inside the application and you can put multiple versions on the same machine
- All work scenarios are supported using console tools.
Let's go to
ASP.NET Core . This is a new Microsoft framework for developing Web applications, which appeared due to the redesign of the previously existing ASP.NET MVC framework. You need to understand that ASP.NET Core does not have to be based on .NET Core. You can create an ASP.NET Core application based on the good old .NET. This option is in the standard dialog of creating a new project:

What, then, are the features and differences between ASP.NET Core and previous ASP.NET? Some of these are:
- The ability to much better control the necessary modules, assembly. For example, there is no tight binding to IIS, System.Web.dll
- Built-in functionality for dependency injection
- Open source
In addition, the Core application is now unified with other types of applications. Now, it includes the Main method in the same way, which is called when the application is started, and that in turn simply starts the Web part. The minimal application looks like this:
public class Program { public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseStartup<Startup>() .Build(); host.Run(); } } public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app) { } }
The Statup class can, to some extent, be described as a new version of Global.asax (This is the class for global configuration of the entire application in the previous version of ASP.NET). Roughly speaking, we can say that the
ConfigureServices method is needed for configuring the container for implementing dependencies and its services, and the
Configure method for configuring the query processing pipeline.
Let's start the practical implementation
In order to better understand all the innovations, let's create an ASP.NET Core application based on .NET Core.

To make your life easier, choose Web Application and change the authentication to Individual User Accounts. Thus, Visual Studio will already generate all the necessary code for the base application.

Let us consider in more detail what is new in ASP.NET Core. From the point of view of development, the whole concept remains the same. The project structure is based on the MVC pattern. To work with the default data, we use the Entity Framework, the logic is described in controller classes, at the presentation level we use the syntax cshtml + new tag tag helpers.
Check the classes Program.cs and Startup.cs, they really look the same as described above. Of course, the Startup class is not completely empty, but already contains functionality for reading the configuration, setting up basic logging, routing, and binding to our database model.
Let's add the database model with entities for creating and passing tests. We will use the following entities: A set of test questions - TestPackage, The question itself (test) - TestItem, Test result - TestResult. An example can be found
here . I am glad that EntityFramework Core already supports most of the functionality and you can fully use Code First migrations.
Add logic
Now that we have a database model, we can start creating logic for our application. The easiest way to create an admin is scaffolding. To do this, right-click on the controller folder and select Add → New Scaffold Item:

Select "MVC Controller with views using the Entity Framework". This template allows us to quickly create a controller and view to manage one specific model. Let's do this trick for TestPackage and TestItem. As a result, we have a ready prototype admin panel for our system. You can run a project and go to the pages of these controllers, just add its name without the word Controller to the end of the address, for example, / testpackages. Of course, everything is not perfect in it, so you need to finish some moments and make them more comfortable.
After creating a model and a simple functional for working with it, you can proceed to the process of passing tests. In general, everything looks simple. The server sends a question, options, we send back the answer. The server saves it. At the end of the test we show the result. Add
code for this behavior.
In general, all that is needed for the test we have.
Basics of Dependency Injection in an ASP.NET Core
An important innovation in the new version of ASP.NET is also the built-in dependency injection mechanism. In 2016, no one will be surprised by the fact that the mechanism for introducing dependencies can be transferred inside the framework. Few serious applications are written without using this approach. DI in ASP.NET Core is implemented fairly basic, but at the same time allows you to solve most of the problems of dependency management.
The container is configured in the ConfigureServices method of Startup. Example:
You may notice that for the database context and the Identity framework there are additional, not typical methods for their registration. This allows them to be configured more flexibly. The extension-methods are very nicely fit into this service registration approach.
After registering services, the easiest way to get from in the code (for example, in controllers) is to simply add a parameter to the type constructor that was registered. Such controllers are by default created by scaffolding.
Depla
Microsoft Azure remains one of the easiest ways to deploy. The most basic settings are enough for us to complete the work. Deploying a website to a server is just as easy - with a few clicks, starting from the context menu on the project file.

findings
The future of the “classic” .NET framework is not yet known, since it is nevertheless more stable and proven, therefore it can exist for quite a long time (hello, Python 2), although the situation of the rapid migration of most developers to the Core version is possible (not this is already .NET and old - only 14 years old).
Very pleased with the stability of the current version of frameworks and tools. Although they are not perfect, but the main thing is that you can fully write code. Microsoft frameworks, as always, are captivating by the fact that if you were familiar with the previous version, you already know more than half of the next one. For a good ASP.NET developer, you can start writing on Core without reading the documentation at all, although many things will not be clear. It is true then, it is difficult to write in ordinary ASP.NET, they quickly get used to the good.
Also surprised by the number of questions on StackOverflow. Questions that relate to basic things already googling perfectly with the prefix ASP.NET Core. Often, the answers to them lead to Github and to the discussions of the developers of the framework itself, which is a completely new experience for the .NET world. Very unusual and interesting.
SourcesPass the test