Main method of ASP.NET Core

Main method of ASP.NET Core

 

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>();
}

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, which after running 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 the CreateDefaultBuilder()method 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 a later article, we will be a substantial use both methods. Many times when we need to deepen the impression.


Welcome to add a personal Micro Signal: Like if thoughts.

I welcome the attention of the public numbers, not only recommend the latest blog for you, there are more surprises waiting for you and resources! Learn together and common progress!

 

Guess you like

Origin www.cnblogs.com/cool2feel/p/11445686.html