ASP . NET Core is a console application that creates a Web server in its main method, and the following is the code in Program.cs:
usingMicrosoft.aspnetcore;usingMicrosoft.AspNetCore.Hosting;namespacewebapplication5{ Public classProgram { Public Static voidMain (string[] args) {buildwebhost (args). Run (); } Public StaticIwebhost Buildwebhost (string[] args) =Webhost.createdefaultbuilder (args). Usestartup<Startup>() . Build (); }}
main method call Webhost.createdefaultbuilder , which follows the builder pattern to create Web application Host . builder defines the Web server (for example, Usekestrel) and the Startup Class (Usestartup) method. In the above example, the Kestrel Web server is automatically assigned. asp.net core " s web host will attempt to run on IIS (if available). Other Web servers, such as Httpsys, can be used by calling the appropriate extension method. usestartup will be described further in the next section.
Iwebhostbuilder is the return value type of the Webhost.createdefaultbuilder method and provides many optional methods. Some of these methods include Usehttpsys for hosting applications in Httpsys, and usecontentroot for specifying the root content directory. The build and Run methods are used to build the Iwebhost object that will host the application and begin listening for HTTP requests.
Startup
The Startup class is where you define the request handling pipeline and where any services needed by the application are C Onfigured. The Startup class must is public and contain the following methods:
The usestartup method on webhostbuilder Specifies the startup class for your application:
The startup class is where you define the request processing pipeline and configure any services required by the application. The startup class must be public and contain the following methods:
public class startup{ // This method gets called by the runtime. Use this method to add services to the container.
//Add service Place
public void Span style= "color: #000000;" > Configureservices (iservicecollection services) {} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
//Configure where the request pipeline
public void Configure (Iapplicationbuilder app) {}}
configureservices defines the services that the application uses (such as the ASP. NET Core mvc,entity Framework core,identity, etc.). Configure defines the middleware in the request pipeline.
A service is a component that is used for common consumption in an application. Services are provided through Dependency injection (DI). The ASP. NET core includes a local control inversion (IoC) container (constructor injection is supported by default). The local container can be replaced with the container of your choice. In addition to loose coupling, DI can also serve the entire application. For example, you can use logging in your application. For more information, see Dependency Injection.
Middleware
in ASP. NET core, you write the request pipeline using middleware. The ASP. NET Core middleware executes asynchronous logic on HttpContext and then calls the next middleware in turn or terminates the request directly. Add a middleware component named "XYZ" by calling the usexyz extension method in the Configure method.
ASP. NET core is equipped with rich built-in middleware:
- static files
- Routing
- Certification
You can use any Owin-based middleware with ASP. NET Core, or you can customize the middleware. For more information, see Middleware and Open Web Interface for. NET (OWIN).
Server
The ASP. NET Core managed model does not directly listen for requests; Instead, it relies on the HTTP server implementation to forward the request to the application. Forwarded requests are packaged as a set of feature objects that you can access through the interface. The application composes this collection into a HttpContext. The ASP. NET core includes a managed, cross-platform Web server called Kestrel. Just like IIS or Nginx. For more information, see Servers and Hosting.
Content Root
Content Root is the base path for any content used by the application, such as views, Razor Pages, and static resources. By default, content root is the same as the application base path for the executable file that hosts the application. You can use webhostbuilder to specify the location of content root .
Web Root
The Web root of your application is a directory in your project that contains public static resources, such as css,javascript and image files. By default, the static file middleware only supplies files from the Web root directory and its subdirectories. See Working with static files for more information. The Web root path defaults to /wwwroot , but you can use webhostbuilder to specify a different location.
Configuration
ASP. NET core uses the new configuration model to handle simple name value (name-value) pairs. The new configuration model is not based on system.configuration or Web. config ; instead, it is extracted from a set of ordered configuration providers. The built-in configuration provider supports a variety of file formats (Xml,json,ini) and environment variables for environment-based configuration. You can also write a custom configuration provider. For more information, see Configuration.
Environments
Environments, such as "development" and "production", are the best ideas in ASP. Set with environment variables. For more information, see Working with multiple environments.
. NET Core vs.. NET Framework Runtime
The ASP. NET core application can select the. For more information, see Choices between. NET core and the. NET Framework.
Overview of ASP. NET Core Principles