New features in the ASP. NET Core 2.0 release

Source: Internet
Author: User
Tags webhost
Amazing ASP. NET Core 2.0, this article focuses on the new features of the ASP. NET Core 2.0 version, and interested partners can refer to

Objective

The change and development speed of the ASP. NET core is fast, and when you find out that you haven't mastered ASP. 1.0, 2.0 is almost ready to release, now 2.0 is in Preview 1, which means that the functionality has been basically determined and has not yet learned the ASP. Ore students can start learning directly from 2.0, but if you have mastered 1.0, then you just need to know some of the features added and modified in 2.0.

The release and upgrade of each major release is always a surprise and exciting feature for developers, and the new features of the 2.0 version of the ASP. NET core are mainly focused on several parts.

Changes to the SDK

PS: Now if you want to experience all of the ASP. NET Core 2.0 features in VS, you need the VS 2017.3 preview version. Of course you can use VS Core to get a quick look.

Download address for. NET Core 2.0 Priview:
Www.microsoft.com/net/core/preview

You can use the following command to view the version in CMD when you are finished.

Change 1: added a new command as indicated by the arrow.

dotnet New razordotnet New nugetconfigdotnet new pagedotnet new viewimportsdotnet new Viewstart

These new CLI commands are added. Where Viewimports,viewstart is the _xxx.cshtml two files in the Razor view.

Change 2: dotnet new xxx will automatically restore the NuGet package without you having to re-dotnet the restore command again.

G:\Sample\ASPNETCore2 > dotnet New mvcthe Template "ASP. NET Core Web App (Model-view-controller)" was created Successfu Lly. This template contains technologies from Parties and other than Microsoft, see HTTPS://AKA.MS/TEMPLATE-3PN 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 resulting Csporj project file is as follows:

Where the Red Arrow section is new, let's look at it in turn:

Mvcrazorcompileonpublish:

In version 1.0, if we need to compile the Views folder in MVC as a DLL at the time of publication, we need to reference
Microsoft.AspNetCore.Mvc.Razor.ViewCompilation This NuGet package, and now no longer required, this feature has been integrated by default in the SDK, only need to add the configuration in Csporj, The *.cshtml file in the Views folder is automatically packaged when it is published as a DLL assembly.

Packagetargetfallback

This configuration item is the target framework that is used to configure the current assembly support.

Usersecretsid

This is used to store the secret used in the program, previously stored in the Project.json file, now you can configure it here.

For more information about usersecrets, you can view this blog post for me.

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, a metadata package that contains a lot of stuff, including: Authorization, Authentication, Identity, CORS, Localization, Logging, Razor, Kestrel etc., in addition to these it also attaches entityframework, SQL Server, Sqlite and other packages.

Some students might think that this would refer to assemblies that are not used in many projects, resulting in a huge post-release program, but let me tell you not to worry that the post-release assemblies will not be large, but will be much smaller because Microsoft integrates all of these dependencies into the SDK. This means that when you install the SDK, the MVC-related packages are already installed on your system.

The advantage is that you don't have to worry about updating nuget packages or deleting them, because a lot of version inconsistencies cause hidden conflict issues, and another benefit is that it's nice for many novices to be 2333, they don't need to know what they're going to get from that NuGet Get the information you need in the package.

The post-publish folder is now so concise: size 4.3M

Post a previous post-release folder you can feel it: size 16.5M

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

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

The new Program.cs and Startup.cs

Now, when creating an ASP. NET Core 2.0 MVC, program and Startup have changed, and they have become:

Program.cs

public class program{public static void Main (string[] args) {buildwebhost (args). Run (); } public static Iwebhost Buildwebhost (string[] args) = Webhost.createdefaultbuilder (args). Usestartup<startup> (). Build ();}

Startup.cs

public class startup{Public Startup (iconfiguration configuration) {configuration = configuration;}  Public iconfiguration Configuration {get;}  public void Configureservices (Iservicecollection services) {services. Addmvc (); } public void Configure (Iapplicationbuilder app, ihostingenvironment env) {if (env. Isdevelopment ()) {app. Usedeveloperexceptionpage (); } else {app. Useexceptionhandler ("/home/error"); } app. Usestaticfiles (); App. USEMVC (routes = {routes. MapRoute (name: "Default", Template: "{controller=home}/{action=index}/{id}"); }); }}

Can be found that the new Program.cs and Startup.cs in the content has become very simple, a lot less such as Appsetting.json file additions, log middleware, Kertrel, hostingenvironment, etc., So what's going on? Other they have been integrated into the webhost.createdefaultbuilder function, then we follow the source to see how the internal is done.

Webhost.createdefaultbuilder

Here is the source code for this function of Webhost.createdefaultbuilder:

public static Iwebhostbuilder Createdefaultbuilder (string[] args) {var builder = new Webhostbuilder (). Usekestrel (). Usecontentroot (Directory.GetCurrentDirectory ()). 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); }} config. Addenvironmentvariables (); if (args! = null) {config. Addcommandline (args); } }) . Configurelogging (hostingcontext, logging) = {logging. Useconfiguration (HostingContext.Configuration.GetSection ("Logging")); Logging. Addconsole (); Logging. Adddebug (); }) . Useiisintegration (). Usedefaultserviceprovider (context, options) = {options. Validatescopes = context. HosTingenvironment.isdevelopment (); }) . Configureservices (Services + = Services. Addtransient<iconfigureoptions<kestrelserveroptions>, kestrelserveroptionssetup> (); }); return builder;}

As you can see, the new approach has hidden a lot of detail and helped us do most of the configuration work. But you know how to come from defining these middleware or configuration is also one of the necessary skills.

Changes in Appsettings.json

In Appsettings.json, we can define Kestrel-related configurations that the application uses to launch Kerstrel when it is started.

{"Kestrel": {"endpoints": {"Localhost": {"Address": "127.0.0.1", "Port": "9000"}, "Localhosthttps": {"Address": "127 .0.0.1 "," Port ":" 9001 "," Certificate ":" HTTPS "}}}," Certificate ": {" Https ": {" Source ":" Store "," storelocation ":" Lo Calmachine "," StoreName ":" MyName "," Subject ":" Cn=localhost "," Allowinvalid ": True}}," Logging ": {" includescopes ": FAL SE, "LogLevel": {"Default": "Warning"}}

The configuration above configures the local address and port used for Kertrel startup, as well as the configuration items for https that need to be used in the production environment, typically the node configuration section on HTTPS should be located in appsettings. The Production.json file.

Now,dotnet run will listen for 9000, and 9001 ports at the same time when it starts up.

Changes to the log

The change in the log in ASP. NET Core 2.0 is very comforting, because it's not part of the MVC middleware configuration, it's part of the Host, which seems a bit awkward and embarrassing. This means that you can record some of the error messages that are generated at the bottom.

Now you can extend the log configuration as such.

public static Iwebhost Buildwebhost (string[] args) = Webhost.createdefaultbuilder (args). Usestartup<startup> (). Configurelogging (factory=>{your configuration}). Build ();

The new Razor Pages

Another exciting feature introduced by ASP. Razor Pages. Provides another way to make your Web page development more immersive programming, or called page-focused. The amount ... It's a bit like the Web Form Page of the past, it's part of the MVC framework, but they don't have a Controller.

You can create a new razor Pages-type application by dotnet The new Razor command.

The cshtml page code for Razor pages might look like this:

@page @{var message = "Hello, world!";} 

Pages in Razor pages must have @page tags. They may also have a *.cshtml.cs class file, corresponding to the page related to some code, is not much like Web Form it?

Some students may ask, no Controller is how to route it? In fact, they navigate through the physical path of a folder, such as:

More information about Razor pages can be found here:
Docs.microsoft.com/en-us/aspnet/core/razor-pages

Summarize

As you can see, there is a lot of convenience and help in our development process in ASP. NET Core 2.0, including improvements to the program, including the integration of MVC-related NuGet packages, including Appsetting.json server configuration, and surprisingly razor Page, is not eager to look forward to the release of the official version of it? If you're looking forward to it, "recommend" Let me know ~ 2333.

If you are interested in ASP. You can pay attention to me, I will regularly share my learning experience on the blog.

"Recommended"

1. ASP. NET free Video Tutorials

2. Detailed description of ASP. mvc--Routing

3. Share an instance of the ASP. NET Core store Secret (User Secrets) in the development environment

4. How to use ref and span<t> to improve program performance in. Net Core Implementation Code

5. Core implementation of a comprehensive literacy paste ASP method detailed

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.