- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the purpose of Program.cs file in C# ASP.NET Core project?
ASP.NET Core web application is actually a console project which starts executing from the entry point public static void Main() in Program class where we can create a host for the web application.
public class Program{ public static void Main(string[] args){ BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<startup>() .Build(); }
The WebHost is a static class which can be used for creating an instance of IWebHost and IWebHostBuilder with pre-configured defaults.
The CreateDefaultBuilder() method creates a new instance of WebHostBuilder with pre-configured defaults. Internally,
it configures Kestrel, IISIntegration and other configurations. The following is CreateDefaultBuilder() method.
- Sets the “Content Root” to be the current directory
- Allows Command Line args to be pushed into your configuration object
- Adds both appsettings.json and appsettings.{Environment}.json to be loaded into the configuration object
- Adds environment variables to the configuration object
- If in Development, allows the loading of secrets.
- Adds Console/Debug loggers
- Tells the app to use Kestrel and to load Kestrel configuration from the loaded config
- Adds Routing
- Adds IIS Integration
When we want to host our application into iis we need to add UseIISIntegration() method specifies the IIS as the external web server.
UseStartup<startup>() method specifies the Startup class to be used by the web host. we can also specify our custom class in place of startup.
Build() method returns an instance of IWebHost and Run() starts web application till it stops.
- Related Articles
- Explain the purpose of the Program class in ASP.NET Core
- Explain the purpose of the Startup class in ASP.NET Core
- What is Kestral C# Asp.net Core?
- What is routing in C# ASP.NET Core?
- What is Metapackage in C# Asp.net Core?
- What is the use of UseIISIntegration in C# Asp.net Core?
- What is the role of IWebHostEnvironment interface in C# ASP.NET Core?
- Explain how to create a new ASP.NET Core project
- Explain the purpose of the .csproj file in an ASP.NET application
- What are the benefits of choosing ASP.NET Core over ASP.NET?
- What is the difference between IApplicationBuilder.Use() and IApplicationBuilder.Run() C# Asp.net Core?
- What is the use of the Configure() method of startup class in C# Asp.net Core?
- What is the AddSingleton vs AddScoped vs Add Transient C# Asp.net Core?
- What are the various JSON files available in C# ASP.NET Core?
- What is ASP.NET Core? Explain how it's different from the ASP.NET framework.
