What is ASP.NET Core, and Program.cs
ASP.NET Core is the framework for building web apps and APIs in C#. This video explains what it is, how Kestrel and the request lifecycle work, walks through Program.cs line by line, builds the first MapGet endpoint, and covers ports, .http files and static files.
38.1 Why is it used?
- One open-source, cross-platform framework (Windows, Linux, macOS) for every kind of web app.
- Minimal APIs and controller-based Web APIs, MVC and Razor Pages, Blazor for interactive UI.
- SignalR for real-time features and gRPC for fast service-to-service calls are part of it too.
- It ships its own web server (Kestrel), so the app runs the same on a laptop, in a container or in the cloud.
38.2 The bug: the app runs, but curl cannot connect
- dotnet new web creates an empty ASP.NET Core project and dotnet run starts it ("Application started").
- An old tutorial says localhost:5000, and curl fails with Could not connect to server.
dotnet new web -n ShopAppcd ShopAppdotnet run$ curl -i localhost:5000curl: (7) Failed to connect to localhost:5000 after 2225 ms: Could not connect to server- Step 1
1.dotnet new web picks a random port (5213 here)
- Step 2
2.It is saved in Properties/launchSettings.json
- Step 3
3.The terminal prints that port on startup
- Step 4
4.Old templates and blogs use localhost:5000
- Step 5
5.Nobody listens on 5000: Could not connect
The port comes from launchSettings.json
38.3 Kestrel and the request lifecycle
- Kestrel is the built-in web server; it runs inside your app process.
- It turns each HTTP request into an HttpContext object that holds both the request and the response.
- In production a reverse proxy (Nginx, a cloud load balancer) often sits in front, but it is optional.
- The request then passes through middleware (logging, static files, auth), routing picks an endpoint, and the response travels back.
- Step 1
1.Client sends an HTTP request
- Step 2
2.Kestrel receives it and creates the HttpContext
- Step 3
3.Middleware pipeline: logging, static files, auth
- Step 4
4.Routing finds the endpoint and its handler runs
- Step 5
5.Response travels back through the pipeline to the client
Middleware and routing get their own videos later
38.4 Program.cs line by line
| Line | What it does |
|---|---|
| WebApplication.CreateBuilder(args) | Creates the builder; sets up configuration, logging and services (DI) |
| builder.Build() | Builds the actual WebApplication |
| app.MapGet("/", ...) | GET on the root URL returns "Hello World!" |
| app.Run() | Starts Kestrel and blocks until the app shuts down |
var builder = WebApplication.CreateBuilder(args);var app = builder.Build(); app.MapGet("/", () => "Hello World!"); app.Run();$ curl -i localhost:5213HTTP/1.1 200 OKContent-Type: text/plain; charset=utf-8Server: Kestrel Hello World!- Older projects had a separate Startup class; since .NET 6 everything lives in this one file (minimal hosting).
38.5 First endpoint: a JSON list
- MapGet("/api/products", ...) returns an array of Product records.
- When a handler returns an object, ASP.NET Core serializes it to JSON automatically (camelCase, application/json).
var builder = WebApplication.CreateBuilder(args);var app = builder.Build(); app.MapGet("/", () => "Hello World!"); app.MapGet("/api/products", () => new[]{ new Product(1, "Mixer Grinder", 2499), new Product(2, "Steel Bottle", 399)}); app.Run(); record Product( int Id, string Name, decimal Price);$ curl -i localhost:5038/api/productsHTTP/1.1 200 OKContent-Type: application/json; charset=utf-8Server: Kestrel [{"id":1,"name":"Mixer Grinder","price":2499}, {"id":2,"name":"Steel Bottle","price":399}]38.6 Port fix: launchSettings.json
- launchSettings.json is only for local development; dotnet run and Visual Studio use its profiles.
- applicationUrl fixes the port (5038); environmentVariables sets ASPNETCORE_ENVIRONMENT to Development.
- In production the port comes from --urls or the ASPNETCORE_URLS environment variable, not this file.
{ "profiles": { "http": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, "applicationUrl": "http://localhost:5038", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } }}$ dotnet runUsing launch settings from Properties\launchSettings.json...Building...info: Microsoft.Hosting.Lifetime[14] Now listening on: http://localhost:5038info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development38.7 Testing with a .http file
- Keep a .http file in the project instead of typing curl every time.
- @host defines a variable; ### separates requests.
- Visual Studio, Rider and the VS Code REST Client show a Send Request link above each request.
- Commit it to git so every endpoint has a ready-made test request.
@host = http://localhost:5038 ### HelloGET {{host}}/ ### Products listGET {{host}}/api/productsAccept: application/jsonGET http://localhost:5038/api/productsHTTP/1.1 200 OKContent-Type: application/json; charset=utf-8Server: Kestrel [{"id":1,"name":"Mixer Grinder","price":2499}, {"id":2,"name":"Steel Bottle","price":399}]38.8 Checkpoint: MapGet after Run
- Run() does not return until the app shuts down, so code after it never executes.
- /api/orders is never registered: 404 Not Found. Register every endpoint before Run.
var builder = WebApplication.CreateBuilder(args);var app = builder.Build(); app.MapGet("/", () => "Hello World!"); app.Run(); app.MapGet("/api/orders", () => "Order list");$ curl -i localhost:5038/api/ordersHTTP/1.1 404 Not FoundContent-Length: 0Server: Kestrel38.9 Static files: wwwroot + UseStaticFiles
- Files for the browser go in wwwroot, but nothing is served without the static files middleware.
- app.UseStaticFiles() makes GET /index.html return 200 OK (text/html).
- MVC templates use MapStaticAssets, which also optimizes files at build time.
var builder = WebApplication.CreateBuilder(args);var app = builder.Build(); app.MapGet("/", () => "Hello World!"); app.Run(); // wwwroot/index.html exists// GET /index.html -> 404 Not Foundvar builder = WebApplication.CreateBuilder(args);var app = builder.Build(); app.UseStaticFiles(); app.MapGet("/", () => "Hello World!"); app.Run(); // GET /index.html -> 200 OK (text/html)38.10 Common mistakes
- Assuming port 5000: check the port printed at startup or in launchSettings.json.
- Mapping endpoints after app.Run(): they never get registered.
- Putting files in wwwroot but forgetting UseStaticFiles.
- Myth: ASP.NET Core only runs on Windows and IIS. That was the old ASP.NET Framework; ASP.NET Core is cross-platform and common in Linux containers.
38.11 Rules
- Order in Program.cs: builder (services) -> Build -> middleware -> endpoints -> Run.
- Fix the local port in launchSettings; set the production port with --urls or an environment variable.
- Browser files only in wwwroot.
- A .http request for every endpoint, committed to git.
38.12 Interview tips
- Kestrel is the built-in cross-platform web server; it receives the request and creates the HttpContext.
- The HttpContext passes through the middleware pipeline; routing selects the endpoint and its handler writes the response.
- The response travels back through the same pipeline to the client.
- Program.cs: the builder sets up services and configuration; after Build you define middleware and endpoints, then Run.