ASP. NET Core Chapter I: Introduction

Source: Internet
Author: User
Tags httpcontext new set

Original English: Introduction to ASP.

About the ASP. NET Core

ASP. NET Core is a new open-source, cross-platform framework that can be used to build modern, web-based cloud applications, such as the Internet of Things, IoT applications, and mobile backend. The ASP. NET core can be run on either the, or the complete. NET Framework, and its architecture provides an optimal development framework for applications that are published to the cloud or run on-premises, consisting of a small, modular component that preserves your flexibility in structuring the solution. You can develop and run your ASP. NET Core applications across platforms on devices such as Windows, Macs, and Linux. The source code for ASP. NET Core is already hosted on GitHub.

Why build ASP. NET Core

The first ASP. NET Preview version was released 15 years ago (2000) as part of the. NET Framework. Since then, thousands of developers have used it to build and run powerful Web applications. Over the years, we've also added and improved a lot of features to it.

ASP. NET core has a lot of architectural changes, which makes it appear more granular and modular. ASP. NET Core no longer relies on the System.Web.dll type library, but instead is a set of fine-grained, highly decomposed nuget packages. This allows you to install the NuGet package on demand to optimize your app. A smaller surface area allows your application to have tight security, reduce maintenance costs, improve performance, and reduce overhead in the pay-as-you-use model.

With ASP. NET Core, you'll get the following basic improvements:

A) build Web UI and Web APIs in a unified scenario

b) Integrated modern client development framework and workflow

c) A cloud-ready environment-based configuration system

d) built-in dependency injection

e) New, lightweight, modular HTTP request pipeline

f) The ability to host applications in IIS or in self-hosted processes.

g) Support for concurrent application versioning based on. NET Core.

h) distributed completely in NuGet package mode

i) a new set of tools to simplify modern web development

j) Develop and run ASP on devices such as Windows, Macs, and Linux. NET application

k) Open source and community focus

Application anatomy

An ASP. NET core application is actually a simple console application, except that it creates a WEB server in the Main method:

Using system;using microsoft.aspnetcore.hosting;namespace aspnetcoreapp{public    class program    {        public static void Main (string[] args)        {            var host = new Webhostbuilder ()                . Usekestrel ()                . Usestartup<startup> ()                . Build ();            Host. Run ();}}}    

Main uses a webhostbuilder that follows the builder pattern to create the Web application host. The generator has a method that defines the Web server (Usekestrel) and specifies the startup class (Usestartup<startup> ()). The above example uses a Kestrel Web server, but you can specify a different type of Web server. More Usestartup-related information will be shown in the next section. Webhostbuilder provides a number of options, including specifying the host for IIS and IIS Express server useiisintegration, specifying usecontentroot for the content root directory, and so on. The build and run methods are used to build an instance of Iwebhost, which will be used to mount the app and start listening for incoming HTTP requests.

Start startup

The startup method on Webhostbuilder is used to specify the startup class for your application.

public class program{public    static void Main (string[] args)    {        var host = new Webhostbuilder ()            . Usekestrel ()            . Usestartup<startup> ()            . Build ();        Host. Run ();}    }

The Startup class is where you define the request processing pipeline, and the service that the application needs to configure. The startup class must be public and must contain the following methods:

public class startup{public    void Configureservices (iservicecollection services)    {    } public    void Configure (Iapplicationbuilder app)    {    }}

A) configureservices define the services used in the application (see Services) (e.g. ASP. NET MVC Core Framework, Entity framework core,identity, etc.)

b) Configure define the middleware in the request processing pipeline

c) View application launch for more information

Services Service

A service is a common component in an application, and a service instance can be obtained through dependency injection. The ASP. NET Core has a simple control inversion container (ioc,inversion of controls) that supports constructor injection by default, but it can easily be replaced with the IOC container you are accustomed to. In addition to its loose occasional benefits, DI (Dependency injection, Dependency injection) also allows services to be available throughout the application. For example, log records (Logging) can be available anywhere in your application. See Dependency injection for more information.

Middleware middleware

in ASP. NET core you will use middleware to organize your request pipeline. The ASP. NET Core middleware executes the synchronization logic in HttpContext, then calls the next middleware sequentially or terminates the request directly. In general, you can use middleware by calling the appropriate extension method usexyz through the Iapplicationbuilder of the Configure method.

The ASP. NET Core itself has a set of middleware:

A) static file Statics files

b) Routing Routing

C) Certification authentication

You can also create your own middleware.

You can use any Owin-based middleware in an ASP. NET Core environment. View. NET Open Web Interface (open Web Interface for. Net,owin) to learn more

Servers Server

The ASP. NET Core host model does not listen directly to the request, it relies on the implementation of the HTTP server to forward the request to the application. Forwarded requests are packaged into a set of functional interfaces, which are then combined into an application HttpContext. ASP. NET Core contains a managed cross-platform Web server named Kestrel that allows you to run applications on typical Web server products such as IIS or Nginx.

Content root directory Contents root

The content root refers to the underlying path of all files used by the application, such as its view and web content. By default, the content root directory is the same as the underlying directory where the executable file of the Mount application is mounted. You can specify its alternate directory through Webhostbuilder.

Web Directory Web root

The Web directory of your application is a directory of common, static resources such as CSS, JS, image files, and so on in your project. Static file middleware works only on files under the web directory and its subdirectories by default. The default Web directory is the < content root >/wwwroot, but you can specify it as a different directory by Webhostbuilder.

Configuring the Configuration

ASP. NET core uses a new configuration model to handle simple name/value pairs. The new configuration model does not depend on System.Configuration and web. config, instead, it pulls the configuration from the sorted configuration provider (config Provider) collection. The built-in configuration provider supports a variety of file formats (Xml,json,ini) and also supports environment variables so that you can use environment-based configurations. You can also write a custom configuration provider.

Review the configuration for more information.

Environmental environment

such as "development environment" and "production environment", these are first-class concepts in ASP. NET Core and can be set through environment variables. View work in multiple environments (working with multiple environments) for more information.

Building Web UI and Web APIs with ASP. NET Core MVC

A) You can create highly decomposed, testable Web applications that follow the MVC pattern. View MVC and testing for more information

b) You can build HTTP services that support multiple formats and fully support content negotiation. View formatted response data for more information

c) Razor provides an output language to create a view

d) Tag helper (tag Helpers) allows server-side code to participate in creating and rendering HTML elements in the razor file

e) with a custom or built-in formatter (json,xml), you can create an HTTP service that fully supports content negotiation.

f) Model binding can automatically map HTTP request parameters to action method parameters

g) Model validation to automate client and server-side validation

Client Development Client-side Development

ASP. NET core is designed to seamlessly integrate a large number of client frameworks, including Angularjs,knockoutjs and Bootstrap. Review client development for more information.

ASP. NET Core Chapter I: 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.