Understanding the Backend Entry Point
Learn about the backend entry points of the project.
We'll cover the following...
An ASP.NET Core app is a console app that creates a web server. The entry point for the app is a method called Main
in a class called Program
, which can be found in the Program.cs
file in the root of the project:
public class Program{public static void Main(string[] args){CreateHostBuilder(args).Build().Run();}public static IHostBuilder CreateHostBuilder(string[]args) =>Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>{webBuilder.UseStartup<Startup>();});}
This method creates a web host using Host.CreateDefaultBuilder
, which configures items such as the following:
The location of the root of the web content
Where the settings are for items, such as the database connection string
The logging level and where the logs are output
We can override the default builder using fluent APIs, which start with Use
. For example, to adjust the root of the web content, we can add the highlighted line in the following snippet:
public static IHostBuilder CreateHostBuilder(string[] args) =>Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>{webBuilder.UseContentRoot("some-path");webBuilder.UseStartup<Startup>();});
The last thing that is specified in the builder is the Startup
class, which we’ll look at below.
Understanding the Startup class
The Startup
class is found in Startup.cs
and configures the services that the app uses, as well as the request/response pipeline. Here, we will understand the two main methods within this class.
The ConfigureServices
method
Serv ...