DEV Community

FakeStandard
FakeStandard

Posted on

I Didn't Understand Program.cs in .NET, So I Wrote This

When I created a new ASP.NET Core Web App (Model-View-Controller) project in .NET 8, I found there were some lines I didn't fully understand. So, I'm going to record what each line does and how it works.

Here's the full code in Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

Enter fullscreen mode Exit fullscreen mode

The first and third lines are important in the project.

// Prepare the app's configuration and services.
var builder = WebApplication.CreateBuilder(args);
Enter fullscreen mode Exit fullscreen mode

This is where the app starts. It creates a WebApplicationBuilder instance and loads configuration, sets up logging, dependency injection and environment settings.

// Build the app; it's ready to handle requests
var app = builder.Build();
Enter fullscreen mode Exit fullscreen mode

This line builds the WebApplication instance based on everything set up in the builder.

// Enables MVC support for the app
builder.Services.AddControllersWithViews();
Enter fullscreen mode Exit fullscreen mode

Registers services needed for MVC application into the DI container. It includes all the necessary services and configuration for MVC. So your application can use the MVC mode.

// In production, make the app more secure and don't expose detailed errors
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}
Enter fullscreen mode Exit fullscreen mode

If the application is running in development mode, the developer exception page will be shown. Otherwise in a non-development environment, it shows a custom error page at /Home/Error.

Then app.UseHsts() enables HSTS, which enforces HTTPS.

Next, methods starting with the word Use configure the middleware. Each middleware is configured separately, such as HTTPS redirection, statice files, routing, and authorization, etc.

// Enforce users to connect via HTTPS
app.UseHttpsRedirection();
Enter fullscreen mode Exit fullscreen mode

This line redirects all HTTP requests to HTTPS automatically.

// Clients can load static files
app.UseStaticFiles();
Enter fullscreen mode Exit fullscreen mode

This line allows serving static files from the wwwroot folder only, such as CSS, JS, images, etc.

// The app can match URLs to the correct controller and action
app.UseRouting();
Enter fullscreen mode Exit fullscreen mode

This line enables the routing system so the app can match incoming URLs to the correct controller and action.

// Check if the user has access rights
app.UseAuthorization();
Enter fullscreen mode Exit fullscreen mode

It checks whether a user is allowed to access a route or resource.

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
Enter fullscreen mode Exit fullscreen mode

This line defines the default route pattern.

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

Finally, this starts the application and begins listening for incoming HTTP requests.

Easy, right? Job done☑️


Thanks for reading!

If you like this article, please don't hesitate to click the heart button ❤️
or follow my GitHub I'd appreciate it.

Top comments (0)