[05] Main method in ASP.NET Core

Main method of ASP.NET Core

Author: Liang Tongming - Microsoft Most Valuable Professionals (Microsoft MVP) 
article will be updated with the release, I get concerned about the latest version of 
this article comes from "scratch learning ASP.NET Core and EntityFramework Core" directory 
Better Video Course effects: cross platform development and combat to master ASP.NET Core EntityFramework Core 

Main method of ASP.NET Core

Recommended viewing video address

In ASP.NET Core project, we have a named Program.csfile. In this document, we have a public static void Main()method.

public class Program
{
    public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); } 
C#

If you have any experience in using traditional .NET Framework, the console application has to know Main () method, which is the entrance of the console program.

But today, we are creating an ASP.NET Core Web application instead of a console application. Therefore, an obvious question comes to mind Yes. Why do we also have a Main()method?

So this knowledge, to remember. ASP.NET Core application was originally started as a console application, and Program.csfile Main()method is the entrance.

Therefore, when executing our application is running, it will find this Main()method to perform configuration and place to start.

This Main()method of configuring asp.net Core and start it, then it becomes a asp.net Core Web applications. So, if you look at tracking Main()method, it calls CreateWebHostBuilder () method to pass command line parameters.

Then you can see, CreateWebHostBuilder () method returns an object that implements the IWebHostBuilder. On this subject, calls the Build()method, it will be our ASP.NET Core applications generated and hosted on the server. Program calls on the server Run() method, after which run a Web application and start to listen for incoming HTTP requests. CreateWebHostBuilder()Static class method calls WebHostthe static method CreateDefaultBuilder()CreateDefaultBuilder()The method has created a pre-set default values on the server. CreateDefaultBuilder()Method of performing multiple operations to create servers.

We discussed in detail in a later video in CreateDefaultBuilder()all methods.

Now you only need to know CreateDefaultBuilder()is the default value creation program configured on the server to exist. It is provided as part of the server, the used IWebHostBuilderinterface UseStartup()extension method to configure Startupclass. If you are not familiar with the concept of extension methods, then you should go to make up classes.

According to Microsoft's rules, the ASP.NET Core startup class name Startup. This class has two methods.

public class Startup
{
    public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } } 
C#

Startup although only two class method, but this method did two very important things:

  • ConfigureServices()Needed to configure the application service method
  • Configure()Request processing pipeline configuration method of application

We understand the role of these two methods is very important.

In later lessons, we will use a lot of both methods. Many times when we need to deepen the impression.

Article Description

If you think my article quality is good, welcome a reward, you can also subscribe to my video Oh, 
is not authorized are not allowed to reprint this article, 52abp.com retain the copyright 

Thank you for supporting me

Guess you like

Origin www.cnblogs.com/wer-ltm/p/11028218.html