ASP.NET Core MVC Folder Structure & Program.cs Explained
ASP.NET Core MVC Folder Structure & Program.cs Explained
Explore how your ASP.NET Core MVC application is organized and how Program.cs bootstraps it all.
Why Folder Structure Matters
A clean project structure makes your MVC application easier to scale, test, and maintain. In this post, we’ll explore each key folder and show you how Program.cs
ties it all together.
ASP.NET Core Folder Breakdown
Folder/File | Purpose |
---|---|
/Controllers | Holds controller classes that handle HTTP requests. |
/Models | Contains business logic and data models. |
/Views | Contains Razor views organized in folders per controller. Includes Shared and layout pages. |
/wwwroot | Static files like CSS, JS, images, and libraries (jQuery, Bootstrap, etc). |
appsettings.json | Stores configuration like DB connection, logging, and custom settings. |
Program.cs | Application entry point. Configures services and middleware. |
💡 Tip: Views are rendered inside the layout page via
@RenderBody()
. Shared layout, header, footer are in /Views/Shared
.
⚠️ Note: Your controller methods return
View()
with a model that the corresponding .cshtml file receives using @model
.
Program.cs & Dependency Injection
Program.cs
is where it all begins. It builds the web host, registers services, and defines middleware execution order.
var builder = WebApplication.CreateBuilder(args); // Register services builder.Services.AddControllersWithViews(); builder.Services.AddScoped(); // Example service var app = builder.Build(); // Configure middleware if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseRouting(); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run();
💡 Register your services using DI so they can be injected into controllers automatically.
📌 Folder Purpose Summary Table
- Controllers: Request handling
- Views: Razor UI rendering
- Models: App logic + data
- wwwroot: Static files
- appsettings.json: Config storage
- Program.cs: Startup + middleware
Comments
Post a Comment