In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces you what are the new features of AmazingASP.NETCore, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Every time a large version is released and upgraded, it always brings some pleasant surprises and exciting features to developers. The new features of this version 2.0 of ASP.NET Core are mainly concentrated in several parts.
Changes in SDK
PS: currently, if you want to experience all the features of ASP.NET Core 2.0 in VS, you need a preview version of VS 2017.3. Of course you can use VS Core to learn quickly.
Download the .NET Core 2.0 Priview from:
Https://www.microsoft.com/net/core/preview
When you are finished, you can use the following command in cmd to view the version.
Change 1: the new command indicated by the arrow in the following figure has been added.
Dotnet new razordotnet new nugetconfigdotnet new pagedotnet new viewimportsdotnet new viewstart
These new cli commands are added. Where viewimports,viewstart is the _ xxx.cshtml file in the Razor view.
Change 2: dotnet new xxx will automatically restore the NuGet package, so you don't need to dotnet restore again.
G:\ Sample\ ASPNETCore2 > dotnet new mvcThe template "ASP.NET Core Web App (Model-View-Controller)" was created successfully.This template contains technologies from parties 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 you create a MVC project, the generated csporj project file is as follows:
Among them, the red arrow part is the new content, let's take a look at it in turn:
MvcRazorCompileOnPublish:
In version 1.0, if we need to compile the Views folder in MVC to DLL at release time, we need to refer to the
Microsoft.AspNetCore.Mvc.Razor.ViewCompilation this NuGet package, but now is no longer needed, this function has been integrated into SDK by default, you only need to add configuration in csporj, and the * .cshtml file in the Views folder will be automatically packaged as DLL assembly when it is released.
PackageTargetFallback
This configuration item is used to configure the target framework supported by the current assembly.
UserSecretsId
This is used to store the secrets used in the program, which was previously stored in the project.json file, but now you can configure it here.
For more information about UserSecrets, you can check out my blog post.
MVC related package
In Core MVC 2.0, all MVC-related NuGet packages are integrated into this Microsoft.AspNetCore.All package, which is a meta-packet that contains a large number 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 release of the program becomes very large, but I want to tell you not to worry, the release of the assembly will not become very large, but will be much smaller, because Microsoft integrates all these dependencies into the sdk, that is, when you install sdk, the MVC-related package has been installed on your system.
This advantage is that you do not have to worry about updating the Nuget package or deleting it, because a large number of version inconsistencies lead to hidden conflict problems, another advantage is that it is very friendly to many novices 2333, they do not need to know when they will get the information they need from that NuGet package.
Now, the released folder is so simple: 4.3m in size
Post a previous post-release folder and feel it: size 16.5m
Some students may wonder where they put the referenced MVC packages, which are located in this directory by default:
C:\ Program Files\ dotnet\ store\ x64\ netcoreapp2.0
New Program.cs and Startup.cs
Now, when creating an ASP.NET Core 2.0 MVC program, Program and Startup have changed, and they have become like this:
Program.cs
Public class Program {public static void Main (string [] args) {BuildWebHost (args). Run ();} public static IWebHost BuildWebHost (string [] args) = > WebHost.CreateDefaultBuilder (args) .UseStartup () .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?}");});}
You can find that the content in the new Program.cs and Startup.cs has become very simple, a lot less, such as the addition of appsetting.json files, logging middleware, Kertrel, HostingEnvironment, etc., so what's going on? Others have been integrated into the WebHost.CreateDefaultBuilder function, so let's follow up the source code to see how it is done internally.
WebHost.CreateDefaultBuilder
The following is the source code of the function WebHost.CreateDefaultBuilder:
Public static IWebHostBuilder CreateDefaultBuilder (string [] args) {var builder = new WebHostBuilder () .UseKestrel (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 ();}); return builder;}
As you can see, the new approach has hidden a lot of details and helped us to 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 the Kerstrel at startup.
{"Kestrel": {"Endpoints": {"Address": "127.0.0.1", "Port": "9000"}, "LocalhostHttps": {"Address": "127.0.0.1", "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 configures the local address and port used when Kertrel starts, as well as the configuration items of HTTPS that need to be used in the production environment. In general, the node configuration section about HTTPS should be located in the appsettings.Production.json file.
Now, dotnet run will listen on both ports 9000 and 9001 when it starts.
Changes in the log
The change in logging in ASP.NET Core 2.0 is very comforting because it is no longer part of the configuration of MVC middleware, but part of Host, which seems a little awkward. This means that you can record some of the error messages generated at a lower level.
Now you can expand the log configuration in this way.
Public static IWebHost BuildWebHost (string [] args) = > WebHost.CreateDefaultBuilder (args) .UseStartup () .ConfigureLogging (factory= > {your configuration}) .build ()
Brand-new Razor Pages
Another exciting feature introduced by ASP.NET Core 2.0 is Razor Pages. Provides another way to make your Web page development more immersive programming, or page-focused. Uh... It's a bit like the old Web Form Page, it's part of the MVC framework, but they don't have Controller.
You can use the dotnet new razor command to create a new application of type Razor Pages.
The cshtml page code for Razor Pages might look like this:
@ page@ {var message = "Hello, World!";}
@ message
The page of Razor Pages must have an @ page tag. They may also have a class file for * .cshtml.cs, and some code related to the corresponding page, is it very much like Web Form?
Some students may ask, how is it routed without Controller? In fact, they navigate through the physical path of the folder, such as:
More information about Razor Pages can be found here:
Https://docs.microsoft.com/en-us/aspnet/core/razor-pages
As you can see, ASP.NET Core 2.0 has brought a lot of convenience and help to our development process, including improvements such as Program, including the integration of MVC-related NuGet packages, including appsetting.json server configuration, and the amazing Razor Page.
What are the new features of AmazingASP.NETCore to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.