Purpose of Startup.cs in ASP.NET Core
π‘ Concept Name
Startup.cs in ASP.NET Core
π Quick Intro
The Startup.cs
file is a foundational component of an ASP.NET Core app. It defines how services are registered (like logging, DB, MVC) and how HTTP requests are handled via middleware.
π§ Analogy / Short Story
Imagine a theater play. Before it starts, you need to cast actors (services) and define stage sequence (request pipeline). Startup.cs
is like the director's guide: who plays what role and when they appear on stage. It ensures everything runs smoothly.
π§ Technical Explanation
Startup.cs
contains two main methods:
- ConfigureServices: Registers application services and dependencies.
- Configure: Defines middleware components that process incoming HTTP requests.
These are called by the ASP.NET Core runtime at app startup and define the appβs behavior.
π― Purpose & Use Case
- β Register services for DI (e.g., controllers, EF Core)
- β Define middleware (e.g., routing, exception handling)
- β Set up environment-specific behaviors (e.g., dev vs prod)
- β Organize startup logic in one place
π» Real Code Example
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}

β Interview Q&A
Q1: What is the role of Startup.cs?
A: It configures services and the HTTP request pipeline.
Q2: What are the two core methods inside Startup.cs?
A: ConfigureServices and Configure.
Q3: What does ConfigureServices do?
A: Registers services to the DI container.
Q4: What does Configure method do?
A: Builds the middleware pipeline.
Q5: Is Startup.cs required in ASP.NET Core 6?
A: No, it's optional due to the new minimal hosting model.
Q6: Where do we register MVC or Razor Pages services?
A: Inside ConfigureServices using AddControllersWithViews or AddRazorPages.
Q7: Can you use environment-specific logic in Startup.cs?
A: Yes, using IWebHostEnvironment inside Configure.
Q8: Where should middleware like UseRouting or UseAuthorization be placed?
A: Inside the Configure method in the correct order.
Q9: How do you inject services in controllers after registering them?
A: Via constructor injection.
Q10: Whatβs the replacement of Startup.cs in .NET 6+?
A: Configuration is now directly written in Program.cs using minimal hosting.
π MCQs
Q1. What is the main purpose of Startup.cs?
- Initialize Razor views
- Create controllers
- Configure services and middleware
- Compile source files
Q2. Which method in Startup.cs registers dependencies?
- Main
- ConfigureServices
- Configure
- Build
Q3. Which method sets up the middleware pipeline?
- ConfigureServices
- Startup
- Configure
- Main
Q4. What does app.UseRouting() do?
- Registers controllers
- Adds MVC services
- Enables endpoint routing
- Configures views
Q5. Where would you call services.AddControllers()?
- Program.cs
- Configure
- Startup constructor
- ConfigureServices
Q6. How can you check for environment inside Startup.cs?
- Using HostingEnv variable
- Using IConfiguration
- Using IWebHostEnvironment
- Using IAppEnvironment
Q7. Which of these is middleware?
- AddMvc()
- MapControllers()
- UseDeveloperExceptionPage()
- AddScoped()
Q8. What is the correct order for middleware?
- Endpoints → Routing → Auth
- Routing → Auth → Endpoints
- Auth → Endpoints → Routing
- None of the above
Q9. Can you register custom services in Startup.cs?
- No
- Yes, via AddControllers
- Yes, via AddRouting
- Yes, via AddScoped/AddSingleton
Q10. What’s a common use of app.UseEndpoints?
- Add authentication
- Set app title
- Configure logging
- Map controller actions
π‘ Bonus Insight
While Startup.cs is optional in .NET 6+, separating configuration into a Startup class improves modularity in large projects. For teams working in layered architectures, it's still a best practice.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!