ASP.NET Core Project Structure

লেসন ১৬ / ৫০

Folder Layout

MyApi/
├── Controllers/
│   └── UsersController.cs
├── Models/
├── Services/
├── Data/
│   └── AppDbContext.cs
├── Migrations/
├── wwwroot/                  # static files
├── Properties/
│   └── launchSettings.json
├── appsettings.json
├── appsettings.Development.json
├── Program.cs                # entry point ⭐
└── MyApi.csproj

Program.cs (Minimal Hosting Model — .NET 6+)

var builder = WebApplication.CreateBuilder(args);

// 1. Service Registration (DI container)
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<AppDbContext>(opt =>
    opt.UseSqlServer(builder.Configuration.GetConnectionString("Default")));
builder.Services.AddScoped<IUserService, UserService>();

var app = builder.Build();

// 2. Middleware Pipeline (order matters!)
if (app.Environment.IsDevelopment()) {
    app.UseSwagger();
    app.UseSwaggerUI();
}

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

app.Run();

appsettings.json

{
  "ConnectionStrings": {
    "Default": "Server=localhost;Database=MyDb;Trusted_Connection=True;"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "Jwt": {
    "Key": "your-secret-key-min-32-chars",
    "Issuer": "MyApi",
    "Audience": "MyApiUsers",
    "ExpiryMinutes": 60
  },
  "AllowedHosts": "*"
}

Environment-specific Config

appsettings.json                  # base
appsettings.Development.json      # dev override
appsettings.Production.json       # prod override

// Environment Variable
ASPNETCORE_ENVIRONMENT=Development

launchSettings.json

{
  "profiles": {
    "MyApi": {
      "commandName": "Project",
      "applicationUrl": "https://localhost:7001;http://localhost:5001",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

Built-in Middleware Order ⭐

1. UseExceptionHandler       # global error
2. UseHsts                   # HTTP Strict Transport
3. UseHttpsRedirection
4. UseStaticFiles
5. UseRouting                # match URL → endpoint
6. UseCors
7. UseAuthentication         # who?
8. UseAuthorization          # allowed?
9. MapControllers            # endpoint execute

Run

dotnet run
dotnet watch run    # auto-reload
# Browser: https://localhost:7001/swagger