New generation development framework for .NET Core configuration Easy

17582589-c947bf0b8057a59a.jpg

  .NET Core is an exciting framework that allows .NET APP cross-platform as possible, but also provides the basis for micro-services, it is possible to provide the technical infrastructure for the distributed system. Now the market introduction of the book .NET Core few and far between, there are only good Pan Chen, obscure, some authors simply copied the estimated MSDN. This article combined with my experience, introduces its configuration for the most popular words, hoping to make themselves, but also give you entry.

1 Introduction

  .NET Core ASP.NET and have a lot of different places, the first is the previously mentioned, the former is cross-platform for building distributed micro-service system, which only runs on WINDOWS system, used to build monolithic APP. Second is the architecture, both of which are not the same in many places, such as in the interception Pipeline HTTP request, the processing of the two is different. The former is called by "Middleware" middleware to intercept processing, while the latter is treated by injecting into each HttpModule Application of event. Finally, development is not the same on both, depends only on the former NuGet package required, the application only needs to obtain the desired components, which large package is mounted based approach.

2.DEMO

Let's look at a DEMO, through manual configuration, step by step to build a .NET CORE MVC application.

   Step 1: Install VS2017, create a WEB application, select Empty Project.


17582589-8fb4c59a2708bad8.jpg


17582589-4e990bf97a681720.jpg

Step two: look at the structure of the project:


17582589-e21f2e03f150546d.jpg

And traditional ASP.NET project structure is not the same, launchSettings.json here replaced the Web.config configuration file, dependency is the project dependent NuGet package, while Program.cs is the project's startup items, which has a main () method , is the entry application running, main method is similar to the console program. Startup.cs is a very important class, the class understood popular application configuration, the desired configuration for application services, and how to define the respective processing request HTTP Pipeline, we look at the following two classes code.

  Step 3: Check Startup and Program

  First, we look Program, which is the entrance to run, well understood, and not the same as traditional .NET:


17582589-84d51712a7f2ee8f.jpg

  Which, CreateWebHostBuilder method for creating a WebHost server listens for requests (simple to understand so you can).

   Then look Startup:


17582589-a12f0b63e44673ec.jpg

There are two methods,

1) ConfigureServices for adding the required service application, which is a set of parameters IServiceCollection see a service, to add the required service. For example, you use MVC, well, put it here MVC service add to the mix: services.AddMvc (), so put the MVC join the service. Like, you want to use EF, you can also use a similar AddDbContext <ApplicationDbContext> method can be applied. You understand that simply add to the services required for the application for this method.

2) Configure for respective HTTP PIPELINE request comes to the above, each event is traditional ASP.NET class APPLICATION by implantation, the .NET CORE is not the same, it is added by a method called here UseXXX the call IApplicationBuilder middleware middleware sequentially handle HTTP requests, as shown below:


17582589-619efda357bb56d6.jpg

Here you simply understood as the traditional Module for ASP.NET.

  Step Four: Start manually configure MVC application:

  1) combined with said third step, we configure Startup:


17582589-076ef1b9c5c02f0d.jpg

 ConfigureServices was added services.AddMvc (), used to increase the MVC service. Configure a corresponding increase in the MVC routing request, app.UseMvc (route => route.MapRoute (name: "Default", template: "{controller = Home} / {action = Index}")).

  2) We thus increasing the Controller and View in the project:


17582589-f5935670e0113d0d.jpg


17582589-69bd1232aa0b0087.jpg
17582589-db713f3ae0fef340.jpg

  Finally F5 to run the successful output: Hello Wang!

  Through the above analysis, we should be able to .NET CORE generally have a basic understanding, and hope to be able to help you, thank you!


17582589-79445b1a719a005c.jpg

Reproduced in: https: //www.jianshu.com/p/e315132e643e

Guess you like

Origin blog.csdn.net/weixin_33905756/article/details/91171110