liveBook · Manning

Program.cs file

As explained in Building Web APIs with ASP.NET Core

Program.cs is the .NET application entry-point source file that can contain host construction, service registration, and middleware configuration.

Program.cs is the entry point for an ASP.NET Core application. It bootstraps the web application, registers services, and configures the HTTP request middleware pipeline. section 1.2.2 section 1.2.1

A typical Program.cs setup proceeds through these responsibilities:

  • Create a WebApplicationBuilder, then use it to build the WebApplication instance. section 1.2.2
  • Register services such as controllers, endpoint metadata, and Swagger generation through builder.Services. section 1.2.2
  • Configure middleware, including development-only Swagger, HTTPS redirection, authorization, and controller mapping. section 1.2.2
  • Call app.Run() to start the application; Run is terminal and is processed last. section 1.2.2
var builder = WebApplication.CreateBuilder(args);    #1
 
// Add services to the container.
 
builder.Services.AddControllers();                   #2
// Learn more about configuring Swagger/OpenAPI
// at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();          #2
builder.Services.AddSwaggerGen();                    #2
 
var app = builder.Build();                           #3
 
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();                                #4
    app.UseSwaggerUI();                              #4
}
 
app.UseHttpsRedirection();                           #4
 
app.UseAuthorization();                              #4
 
app.MapControllers();                                #4
 
app.Run();                                           #5

Use the annotated template to follow the setup sequence from builder creation through service registration, environment checks, middleware configuration, controller mapping, and application startup. section 1.2.2

Restricting Swagger to selected environments

A board-game API can use an app-setting value such as UseSwagger to decide whether Swagger middleware and its user interface are added. Updating Program.cs to apply that condition prevents Swagger from being enabled when the setting is false. section 2.5.3

ASP.NET Core naming conventions make configuration concise: service extensions commonly begin with Add, while middleware extensions use Use, Map, or Run. The minimal hosting model combines responsibilities formerly separated between Program.cs and Startup.cs. section 1.2.2 section 1.2.1

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains the setup sequence and naming conventions, stopping short of detailed middleware implementation internals.
Examples
Uses the ASP.NET Core template, controller registration, Swagger setup, HTTPS redirection, authorization, and conditional Swagger configuration.
What it assumes
Assumes basic ASP.NET Core project familiarity while introducing services, middleware, and dependency registration through the startup sequence.
What you can do after
Operate/configure an ASP.NET Core API startup file and conditionally enable middleware such as Swagger.
How it connects
Connects the entry point with services, dependency injection, middleware, minimal hosting, configuration, and Startup.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.