NET Core Introduction

Source: Internet
Author: User
Tags what is asp

NET Core Introduction

    • 1. Preface
    • 2. Introduction to ASP.
      • 2.1 What is ASP. NET Core
      • Features of the 2.2 ASP. NET Core
      • 2.3 ASP. NET Core Project folder interpretation
        • 2.3.1 Project Folder Overview
        • 2.3.2 Project.json and Global.json
        • 2.3.1 Properties--launchsettings.json
        • 2.3.4 Startup.cs
          • (1) Constructors
          • (2) Configureservices
          • (3) Configure
        • 2.3.5 Bundleconfig.json
        • 2.3.6 Wwwroot and Bower.json
        • 2.3.7 appsettings
    • 3. References
1. Preface

As a. NET web Developer, the saddest part of my project development deployment is the poor solution on Windows Server, the same artifact Nginx,win on Linux, and you might say " Why don't you use the NLB that comes with Windows? "That's the herd mentality of my little bird, don't you see Stack Overflow 2016 the latest architecture, the load and cache technology used in Linux is already a mature solution. When there is no way to find a suitable solution is a good thing, when there are ways to choose the best solution.

Fortunately,. The emergence of ASP. NET core, which follows the trend of open source, from the people who have been criticized for the win Server, the cross-platform version of ASP appeared in our eyes. For the moment, regardless of the benchmark performance comparisons, we will not be able to compete with java,php Web apps in the future, but at least for us. NET platform developers, we have one more development direction and one more opportunity to try cutting-edge mature technology. So-called 工欲善其事, its prerequisite, let's take a look at what ASP.

2. Introduction to ASP. 2.1 What is ASP . NET Core

ASP. NET Core is a new open source and cross-platform framework for building modern, cloud-based applications such as WEB applications, Internet of Things (IoT) applications, and mobile back-end applications. The ASP. NET Core app can run on top of the. NET core and the full. NET Framework. It integrates the MVC and WEBAPI frameworks in the original ASP. NET, you can develop and run your ASP. NET Core applications across platforms on Windows, Macs, and Linux.

features of the 2.2 ASP. NET Core

ASP. NET Core has made some changes to the architecture that make it a leaner and more modular framework. As we can see in the Project.json file, the ASP. NET Core is no longer based on System.Web.dll (most of what we see in Project.json is Microsoft), based on a series of granular, well-built The NuGet package, combined with smart hints, allows you to optimize your app by simply including the NuGet package you need. A smaller application interface benefits from models that "pay only for the functionality you need" (Pay-for-what-you-use) include more reliable security, streamlined services, improved performance, and reduced costs.

Tips: Starting this application via Ctrl+f5 (non-debug mode) allows you to make code changes, save files, refresh the browser, and then view the code changes. Many developers prefer to use non-debug mode to quickly launch applications and view changes.

Several other improvements are listed below

    • Open source and cross-platform
    • Meet running on the. NET core and. NET Framework
    • Middleware support
    • Performance optimization
    • Ubiquitous Dependency Injection
    • Standard log records
    • Integrate MVC and Web APIs into one framework
    • MVC tags Help
    • CLI Tools
2.3 ASP. NET Core project folder interpretation

Since the release of ASP. NET Core 1.0, the operation of the new project has changed greatly compared to the behavior of the traditional project code release, such as resolving dependencies, choosing the running platform and runtime and so on, even the project structure has changed a lot. More and more configuration options are forwarded by the editor to the developer for manual decision, which is particularly noticeable in the new configuration files, so here's a quick explanation.

Tips: By the way, Upadte3, the latest. NET core projects, there are still a lot of obvious bugs in visual operations.

2.3.1 Project Folder overview

2.3.2 Project.json and Global.json

Project.json is the most important configuration file in a. NET core project, similar to the. csrpoj file on the. NET Framework (in the next release,. NET core will discard the file and revert back to. csrpoj). So here is the next big blog, including the interpretation of Global.json.
Project.json, what do you sell in this gourd?

2.3.1 Properties--launchsettings.json

As the name implies-launch the configuration file. The launchSettings.json file holds unique configuration criteria for an ASP. NET core application for application startup preparation, including environment variables, development ports, and so on. The configuration changes in the file are the same as those launchSettings.json submitted in the developer right-click Project-Properties (the property in the right-click attribute is really very poor), and synchronous updates are supported.

{"Iissettings": {#选择以IIS Express launch"WindowsAuthentication":False, #是否启用windows身份验证"Anonymousauthentication":True, #是否启用匿名身份验证"Iisexpress": {"ApplicationUrl":"http://localhost:24269/", #IIS Express random Port"Sslport":0}},"Profiles": { "IIS Express": { "CommandName": " Launchbrowser ": true,  "Environmentvariables": { "aspnetcore_environment":  "development"},  "WebApplication": {#选择本地自宿主启动, see Program.cs file. Deleting this node will also cause the Visual Studio startup option to be missing  "CommandName":  "Project", #  "Launchbrowser": true,  "LAUNCHURL": Span class= "hljs-string" > "http://localhost:5000", #本地自宿主端口  "Environmentvariables": { Span class= "hljs-string" > "aspnetcore_environment":  "Development"}}}}   

2.3.4 Startup.cs

The Startup.cs file is an ASP. NET Core boot entry file, you must have tried Owin development will not be unfamiliar. When the project runs, the compiler automatically finds the Startup.cs file read startup configuration in the assembly. In addition to constructors, it can define configure and Configureservices methods.

(1) Constructors

Used to start configuration files

Public Startup (Ihostingenvironment env) {var builder =New Configurationbuilder (). Setbasepath (env. Contentrootpath). Addjsonfile ("Appsettings.json", Optional: true, Reloadonchange: true). Addjsonfile ($"appsettings.{ Env. Environmentname}.json ", Optional: true). Addenvironmentvariables (); if (env. Isdevelopment ()) //Read environment variable is development, defined in Launchsettings.json { //This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately. Builder. Addapplicationinsightssettings (DeveloperMode: true);} Configuration = Builder. Build (); }
(2) configureservices

Configureservices is used to configure the various services in our application, which takes a iservicecollection instance with parameters and optionally returns IServiceProvider. The Configureservices method needs to be called before Configure. Our Entity Framework services, or developer-defined dependency injection (ASP. NET core comes with Dependency injection is ubiquitous), see official documentation for more information

public void ConfigureServices(IServiceCollection services) { // Add framework services. services.AddApplicationInsightsTelemetry(Configuration); services.AddMvc(); }
(3) Configure

The Configure method is used to process various middleware in our program, which determines how our application responds to every HTTP request. It must receive a Iapplicationbuilder parameter, we can manually supplement the Iapplicationbuilder use extension method, add middleware to configure, to meet our needs.

public void Configure(Iapplicationbuilder app, Ihostingenvironment env, Iloggerfactory loggerfactory) {Loggerfactory.addconsole (Configuration.getsection ("Logging")); Loggerfactory.adddebug (); app. Useapplicationinsightsrequesttelemetry (); if (env. Isdevelopment ()) {app. Usedeveloperexceptionpage (); App. Usebrowserlink (); } else {app. Useexceptionhandler ("/home/error");} app. Useapplicationinsightsexceptiontelemetry (); App. Usestaticfiles (); App. USEMVC (routes = //MVC routing configuration {routes. MapRoute (name: "Default", Template: "{controller=home}/{action=index}/{id}");});    

2.3.5 Bundleconfig.json

bundleconfig.jsonis a collection of compressed package files (This is not very clear), here is an article Bundleconfig.json specs, the main idea is that it can automatically compress the associated files used in the project, such as generation <script> and <link> symbols.

2.3.6 wwwroot and Bower.json

wwwrootis a folder that holds static content, storing files such as css,js,img. Just mentioned that the new ASP. NET core makes development flexibility greatly improved, the file configuration is also mainly manual, so since there are files stored, there is wwwroot also a file reference bower.json :

{  "name": "asp.net",  "private": true,  "dependencies": { "bootstrap": "3.3.6", "jquery": "2.2.0", "jquery-validation": "1.14.0", "jquery-validation-unobtrusive": "3.2.6" }}

bower.jsonRecords the relevant file references for the project, we can freely delete the required files, such as Jquery.form.js,bower Configuration Manager will automatically help us to download the relevant files on GitHub, the downloaded files will also be placed in the wwwroot folder. These changes can be visually viewed on the project's "dependencies".

Tips: There can only be one Bower.json profile per project, for bower.json more information see bower--Managing your client dependencies

2.3.7 appsettings

It is also the name of the application configuration, similar to the Web. config file on the. NET framework, where developers can write system parameters in a appsettings file (such as a connection string for a program) by means of a key-value pair. The startup class also uses the following code in the constructor to enable the program to recognize the file

new ConfigurationBuilder()                .SetBasePath(env.ContentRootPath)                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddEnvironmentVariables();
3. References
    • Unbundling scripts for debugging
    • How does the Visual Studio 2015 development ASP. NET 5 change?
    • Docs»fundamentals»configuration
    • Bundlerminifier
    • Project.json
    • ASP. NET Core Chinese document middleware

NET Core Introduction

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.