DEV Community

Cover image for Exploring the Program.cs File: Key Components and Configuration in ASP.NET Web API
Emre Kocadere
Emre Kocadere

Posted on

2 1

Exploring the Program.cs File: Key Components and Configuration in ASP.NET Web API

The Program.cs is the main class for .NET applications. It starts the application and allows us to configure the application.
It allows us to add things like dependencies, configuration files, and middlewares.

This is a default Program.cs file for ASP.NET.

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();
app.MapControllers();

app.Run();
Enter fullscreen mode Exit fullscreen mode

Key Components.

1-WebApplication:creates the WebApplicationBuilder instance necessary for your application and performs the basic configurations.

2-WebApplicationBuilder:It is used to start ASP.NET Core applications and to configure the application's settings and services.
You can add dependencies and services using the Services property of the WebApplicationBuilder class.It is used to add services to the dependency injection container. These services provide the objects needed throughout the application.
For Example

builder.Services.AddDbContext<UserManagementContext>(options => options.UseNpgsql(connectionString));

builder.Services.AddScoped<GuidanceService>();
Enter fullscreen mode Exit fullscreen mode

In these lines,
builder.Services.AddDbContext(options => options.UseNpgsql(connectionString)); registers the UserManagementContext with the dependency injection container using Npgsql for PostgreSQL.
builder.Services.AddScoped(); This line registers the GuidanceService as scoped in the dependency injection container, meaning a new instance is created for each request.

builder.Services represents an instance of IServiceCollection,
IServiceCollection is used to register dependencies (services) in your application.
Its official description is as follows:
"A collection of services for the application to compose. This is useful for adding user-provided or framework-provided services."

Also, you can add and read configuration files using the WebApplicationBuilder.

builder.Configuration.AddJsonFile("ConnectionString.json");

var connectionString = builder.Configuration["ConnectionString"];

Enter fullscreen mode Exit fullscreen mode

This code snippet adds the ConnectionString.json file to the configuration and then retrieves the value associated with the "ConnectionString" key from the configuration.

3-var app = builder.Build();:This line creates a WebApplication instance using all the services configured above. This is a fundamental step for building and starting the application.

Additionally, you can add middleware using the app object, which is an instance of the WebApplication. For example, middleware like app.UseHttpsRedirection();, app.UseAuthorization();, and app.MapControllers(); has been added here.

The application's startup process begins at the builder.Build() line. The application's execution starts at the app.Run() line.

Billboard image

Synthetic monitoring. Built for developers.

Join Vercel, Render, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay