Amazing ASP.NET Core 2.0



The change and development speed of core is fast. When you find that you have not mastered core 1.0, 2.0 is about to be released. At present, 2.0 is in Preview 1, which means that the function has been basically determined. Students who have not studied core can start from 2.0 directly, but if you have mastered 1.0, Then you only need to understand some functions added and modified in 2.0.

Each release and upgrade of a large version will always bring some surprises and exciting features to developers. The new features of core version 2.0 are mainly focused on several parts.

SDK changes

PS: at present, if you want to experience all the features of core 2.0 in VS, you need the preview version of vs 2017.3. Of course, you can use vs core to quickly understand.

Download address of. Net core 2.0 Preview:

After completion, you can use the following command in CMD to view the version.

Change 1:A new command is added as indicated by the arrow in the following figure.

dotnet new razor
dotnet new nugetconfig
dotnet new page
dotnet new viewimports
dotnet new viewstart

These new cli commands have been added. Where, viewimports and viewstart are the views in the razor view_ Xxx.cshtml those two files

Change 2:Dotnet new XXX will automatically restore the nuget package. You don’t need to use the dotnet restore command again.

G:\Sample\ASPNETCore2 > dotnet new mvc

The template "ASP.NET Core Web App (Model-View-Controller)" was created successfully.
This template contains technologies from parties other than Microsoft, see for details.

Processing post-creation actions...
Running 'dotnet restore' on G:\Sample\ASPNETCore2\ASPNETCore2.csproj...
Restore succeeded.

*. csproj project file

In 2.0, when creating an MVC project, the generated csporj project file is as follows:

Among them, the red arrow is the new content. Let’s take a look at it in turn:


In version 1.0, if we need to compile the views folder in MVC as a DLL at the time of release, we need to reference it
Microsoft.AspNetCore.Mvc.Razor.ViewCompilationThis nuget package is no longer needed. This function has been integrated into the SDK by default. You only need to add a configuration in csporj. When publishing, the *. Cshtml file in the views folder will be automatically packaged as a DLL assembly.


This configuration item is used to configure the target framework supported by the current assembly.


This is used to store the secrets used in the program. It was previously stored in the project.json file. Now you can configure it here.

For more information about usersecrets, see myThis blog post

MVC related packages

<PackageReference Include=”Microsoft.AspNetCore.All” Version=”2.0.0-preview1-final” />

In core MVC 2.0, all MVC related nuget packages are integrated into this microsoft.aspnetcore.all package. It is a metadata package that contains a lot of things, including authorization, authentication, identity, CORS, localization, logging, razor, kestrel, etc. in addition to these, it also adds entityframework, sqlserver, SQLite and other packages.

Some students may think that this will reference many assemblies that are not used in the project, resulting in the large size of the published program. However, I want to tell you not to worry. The published assembly will not become large, but will be much smaller, because Microsoft integrates all these dependencies into the SDK, that is, after you install the SDK, MVC related packages have been installed on your system.

This has the advantage that you don’t have to worry about updating or deleting the nuget package, because a large number of version inconsistencies lead to hidden conflicts. Another advantage is that it is very friendly to many novices 2333. They don’t need to know when they will get the information they need from the nuget package.

Now?The published folder is so simple: size 4.3m

Post another folder after the previous release. Feel it: size 16.5m

Some students may wonder where they put the referenced MVC packages. By default, they are located in this directory:

C:\Program Files\dotnet\store\x64\netcoreapp2.0

New program.cs and startup.cs

Now, when creating an core 2.0 MVC program, the program and startup have changed. They have become like this:


public class Program
 public static void Main(string[] args)

 public static IWebHost BuildWebHost(string[] args) =>


public class Startup
 public Startup(IConfiguration configuration)
 Configuration = configuration;
 public IConfiguration Configuration { get; }
 public void ConfigureServices(IServiceCollection services)

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 if (env.IsDevelopment())


 app.UseMvc(routes =>
 name: "default",
 template: "{controller=Home}/{action=Index}/{id?}");

It can be found that the contents in the new program.cs and startup.cs have become very simple and much less, such as the addition of appsetting.json file, logging middleware, kertrel, hostingenvironment, etc. so what’s going on? Others they have been integrated intoWebHost.CreateDefaultBuilder In this function, let’s follow up the source code to see how it is done internally.


The following is the source code of webhost.createdefaultbuilder function:

public static IWebHostBuilder CreateDefaultBuilder(string[] args)
 var builder = new WebHostBuilder()
 .ConfigureAppConfiguration((hostingContext, config) =>
 var env = hostingContext.HostingEnvironment;

 config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
  .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);

 if (env.IsDevelopment())
 var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
 if (appAssembly != null)
  config.AddUserSecrets(appAssembly, optional: true);


 if (args != null)
 .ConfigureLogging((hostingContext, logging) =>
 .UseDefaultServiceProvider((context, options) =>
 options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
 .ConfigureServices(services =>
 services.AddTransient<IConfigureOptions<KestrelServerOptions>, KestrelServerOptionsSetup>();

 return builder;

It can be seen that the new method has hidden many details and helped us complete most of the configuration work. But you know how to customize these middleware or configuration is also one of the necessary skills.

Changes in appsettings.json

In appsettings.json, we can define the configuration related to kestrel, and the application will use this configuration to start kestrel at startup.

 "Kestrel": {
 "Endpoints": {
 "Localhost": {
 "Address": "",
 "Port": "9000"
 "LocalhostHttps": {
 "Address": "",
 "Port": "9001",
 "Certificate": "Https"
 "Certificate": {
 "HTTPS": {
 "Source": "Store",
 "StoreLocation": "LocalMachine",
 "StoreName": "MyName",
 "Subject": "CN=localhost",
 "AllowInvalid": true
 "Logging": {
 "IncludeScopes": false,
 "LogLevel": {
 "Default": "Warning"

The above configuration content configures the local address and port used when kertrel is started, as well as the HTTPS configuration items that need to be used in the production environment. Generally, the node configuration part of HTTPS should be located in the appsettings.production.json file.

Now?dotnet runAt startup, it will listen to ports 9000 and 9001 at the same time.

Log changes

In core 2.0, the change of log is very gratifying, because it is not a part of MVC middleware configuration, but a part of host. This sentence seems a little awkward and embarrassing ~. This means that you can record some error messages generated at the lower level.

Now you can extend the log configuration in this way.

public static IWebHost BuildWebHost(string[] args) =>
 . configurelogging (factory = > {your configuration})

New razor pages

Another exciting feature introduced by core 2.0 is razor pages. It provides another way to make you more immersive programming in web page development, or page focused. Well… It’s a bit like the previous web form page. It’s part of the MVC framework, but they don’t have a controller.

You can passdotnet new razorCommand to create a new application of razor pages type.

The cshtml page code of razor pages may look like this:


 var message = "Hello, World!";


The page of razor pages must have@page Mark. They may have another one*.cshtml.cs Is the class file and some code related to the corresponding page very similar to web form?

Some students may ask, how to route without controller? In fact, they navigate through the physical path of the folder, such as:

More information about razor pages can be found here:


It can be seen that core 2.0 has brought a lot of convenience and help to our development process. They include the improvement of program, including the integration of MVC related nuget package, including the server configuration of appsetting.json and the amazing razor page. Have you been eager to wait for the release of the official version? If you expect, click [recommend] to let me know ~ 2333

If you are interested in core, you can follow me. I will regularly share my learning experience in my blog.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.