Home  >  Article  >  Backend Development  >  What is the purpose of the Program.cs file in a C# ASP.NET Core project?

What is the purpose of the Program.cs file in a C# ASP.NET Core project?

王林
王林forward
2023-09-16 14:21:02825browse

C# ASP.NET Core 项目中 Program.cs 文件的用途是什么?

ASP.NET Core web application is actually a console project that starts execution.

Starting from the entry point public static void Main() in the Program class, we can create a Hosting web applications.
public class Program{
   public static void Main(string[] args){
      BuildWebHost(args).Run();
   }
   public static IWebHost BuildWebHost(string[] args) =>
   WebHost.CreateDefaultBuilder(args)
   .UseStartup<startup>()
   .Build();
}

WebHost is a static class that can be used to create instances of IWebHost and IWebHostBuilder with preconfigured default values.

CreateDefaultBuilder() Method creates a new instance of WebHostBuilder with preconfigured default values. Internally,

it configures Kestrel, IISIntegration and other configurations. The following is CreateDefaultBuilder() method.

  • Set the "Content Root" to the current directory
  • Allow command line arguments to be passed to the configuration object
  • Load appsettings.json and appsettings.{Environment}. json two configuration files
Translate the following content into Chinese, retain the html code, and do not add new content:
  • Add environment variables to the configuration object
  • If you are in a development environment, allow the key to be loaded.
  • Add console/debug logger
  • Tell the application to use Kestrel, and load the Kestrel configuration from the loaded configuration config
  • Add routing
  • Add IIS integration
  • When we want to host the application into IIS, we need to addUseIISIntegration()Method that specifies IIS as the external web server.

    UseStartup() method specifies the startup class to be used by the web host. We can also specify our custom class in the startup location.

    Build()The method returns an IWebHost instance, while the Run() method starts the web application until it stops.

    The above is the detailed content of What is the purpose of the Program.cs file in a C# ASP.NET Core project?. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete