Complete C# Tutorial

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 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?

  1. CreateBuilder() sets up the app (logging, config, services, etc).

  2. AddRazorPages() adds support for Razor Pages.

  3. Build() creates the app.

  4. MapRazorPages() maps the endpoints.

  5. 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:

FeatureProgram.csStartup.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! ๐Ÿ’ช

Leave a Comment

Share this Doc

Program.cs and Startup.cs Explained

Or copy link