Explain Program.cs and Startup.cs in ASP.NET Core
👋 Introduction
Hey friend! 👋
Feeling confused about that Program.cs and Startup.cs thing in your ASP.NET Core project? You’re not alone! 🙋♂️🙋♀️
When you’re new, those files look kinda scary. 😅 But trust me—they’re super simple once someone explains it the right way. And guess what? That’s what I’m here for! 💪
So, today, we’ll explain Program.cs in ASP.NET Core and also explain Startup.cs in ASP.NET Core with easy words, short examples, and fun explanations.
Let’s break it down together! 🚀
📚 What You Are Going to Learn in This Lesson
✔️ What is Program.cs and why it matters
✔️ What is Startup.cs and what it does
✔️ The role of both in starting your app
✔️ A real example of both files
✔️ How they work together like best buddies
✔️ Tips to avoid common mistakes
Feeling ready? Let’s jump in! 🤸♀️🤸♂️
🧠 What is Program.cs?
Let’s explain Program.cs in ASP.NET Core in simple words.
Imagine Program.cs as the entry door to your app. 🚪
When someone runs your app, this file is the first thing that executes. It sets up the base for your app to start running.
Here’s a tiny version of what Program.cs looks like in .NET 6 or later:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
var app = builder.Build();
app.MapRazorPages();
app.Run();
👉 This is called minimal hosting model, introduced in .NET 6.
What’s happening here?
-
CreateBuilder()sets up the app (logging, config, services, etc). -
AddRazorPages()adds support for Razor Pages. -
Build()creates the app. -
MapRazorPages()maps the endpoints. -
Run()starts the app. 🚀
Pretty neat, right? 😄
🧠 What is Startup.cs?
Now let’s explain Startup.cs in ASP.NET Core like we’re talking over coffee ☕
Startup.cs is where you configure services and middleware for your app.
In older versions (before .NET 6), your Program.cs looked like this:
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>();
});
}
And then your Startup.cs looked like this:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
So yeah, it handles how your app behaves and what it uses.
🤔 Program.cs vs Startup.cs
Still with me? 😄 Let’s compare both in plain English:
| Feature | Program.cs | Startup.cs |
|---|---|---|
| Runs First? | ✅ Yes | ❌ No |
| App Entry Point | ✅ Yes | ❌ No |
| Adds Services? | ✅ Now it does | ✅ Previously |
| Configures Middleware? | ✅ Now it does | ✅ Previously |
| Required in .NET 6+ | ✅ Yes | ❌ Optional |
So in newer .NET Core versions (like .NET 6+), Program.cs does it all. It’s like a multitasker. 😎
But if you’re using .NET 5 or earlier, you’ll still need to explain Startup.cs in ASP.NET Core because it plays a big role.
🛠 Real Example (For .NET 6 and Later)
Here’s a full working example using just Program.cs (no Startup.cs needed anymore):
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages(); // Register services
var app = builder.Build();
app.UseStaticFiles(); // Middleware to serve static files
app.UseRouting(); // Routing middleware
app.MapRazorPages(); // Endpoint mapping
app.Run(); // Launch the app
Pretty cool, right? 😎 Just one file, clear and simple.
💡 Why Should You Care?
If you understand Program.cs and Startup.cs, you can:
✅ Customize how your app behaves
✅ Add and manage services like databases
✅ Set up authentication, logging, and more
✅ Avoid beginner mistakes
So, it’s really worth the time to explain Program.cs in ASP.NET Core and explain Startup.cs in ASP.NET Core to yourself in your own words too. 🧠💭
🧭 Next What?
Boom! 💥 You just unlocked another key concept! Now you can easily explain Program.cs in ASP.NET Core and explain Startup.cs in ASP.NET Core like a pro. 🎉
But don’t stop now… we’ve got more awesomeness coming up!
👉 In the next chapter, you’ll learn What is Middleware in ASP.NET Core. It’s gonna be fun, simple, and super useful!
See you there, my coding buddy! 👋😄 Keep building! Keep learning! 💪
