estructura-de-un-proyecto-aspnet

Structure of an ASP.NET Project

  • 5 min

When starting a project in ASP.NET Core, one of the first questions that arises is: how do I organize all this?

There is no single “correct” way to do it. But let’s take a look at what is more or less “normal.”

If you create a project with the API template in .NET, the default structure proposed by Microsoft looks something like this 👇

📂 MyApiProject/
├── 📂 Controllers/        # API Controllers
├── 📂 Services/           # Business logic
├── 📂 Models/             # Data models
├── 📂 DTOs/               # Data Transfer Objects
├── 📂 Properties/         # Launch configs
│   └── 📄 launchSettings.json
├── 📂 wwwroot/            # Static files
├── 📄 MyFirstProject.csproj
├── 📄 Program.cs          # Entry point
└── 📄 appsettings.json    # Configuration

In this article, we will see the function of each of these elements, as a starting point to understand how an ASP.NET project works.

When you have more experience, you can make adaptations or changes to tailor it to your preferences and the needs of your project.

Key Files

Some of the most important files you will find in any ASP.NET project are:

The file MyProject.csproj (or whatever name you gave your project) is the .NET project file. It contains a list of dependencies and project settings.

Program.cs is the entry point. Here you define how your app starts, what services are loaded, and how the middleware pipeline is constructed.

var builder = WebApplication.CreateBuilder(args);

// Service configuration
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// HTTP pipeline configuration
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();

app.Run();
  • WebApplication.CreateBuilder(args): Creates a web application builder.
  • app.MapGet(”/”, () => “Hello, world!“): Defines a GET route at the root that returns “Hello, world!“.
  • app.Run(): Starts the application.

In appsettings.json, you will find the configurations: database connections, logging, keys, etc. It is the file where you adjust your application without having to recompile.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "Server=myServer;Database=myDatabase;User Id=myUser;Password=myPassword;"
  }
}
  • Logging: Configures the logging levels.
  • AllowedHosts: Specifies the allowed hosts (* allows all).
  • ConnectionStrings: Defines connection strings for databases.

This file defines how the application is launched in different environments (development, production, etc.). Very useful when working locally with Visual Studio or the .NET CLI.

{
  "profiles": {
    "MyFirstProject": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
  • commandName: Indicates how the application runs (Project to use dotnet run).
  • launchBrowser: Automatically opens the browser when the application starts.
  • applicationUrl: Defines the base URL of the application.
  • environmentVariables: Sets environment variables, such as the runtime environment (Development).

Previously, there was the classic Startup.cs for configuring services and middlewares. Since .NET 6 onwards, everything has merged into Program.cs to simplify things.

Folder Structure

In addition to the configuration files, our ASP.NET project will have a more or less complicated folder structure (depending on our project).

This structure is designed to facilitate code organization and separation of responsibilities. Some of the most common are:

The “wwwroot” folder is the entry point for the static files of the web application, such as images, CSS files, and JavaScript.

This folder contains resources that will be publicly accessible through the browser.

The “Controllers” folder contains the controller classes of the web application. These classes are responsible for handling incoming requests and generating corresponding responses.

Each controller can contain different action methods that define the various routes and actions that the application can handle.

The “Views” folder contains the views of the web application.

This folder contains the HTML or Razor files that define the structure and design of the web pages.

The “Models” folder is used to store the model classes of the web application.

A model represents the data used in the application and defines its structure and behavior.

Models can be used in both controllers and views to interact with the data.

The “Data” folder is used to store classes and files related to data access in the web application.

Here you can find database context classes, migration files, and other components related to database access.

The “Services” folder is used to store the service classes of the web application.

Services are reusable components that encapsulate business logic and are used to perform specific tasks in the application.

These services can be injected into controllers or other components of the application.