ASP.NET Tutorial menu

ASP.NET Project Structure

Before writing pages, it helps to know what each file in a new project does.

Create a Razor Pages Project

Example

bash
dotnet new webapp -o SchoolSite
cd SchoolSite
dotnet watch

The Files and Folders

ItemWhat it is for
Program.csThe starting point: sets up services and the request pipeline
Pages/Your Razor Pages (.cshtml files and their .cshtml.cs code)
Pages/Shared/_Layout.cshtmlThe shared layout: header, menu, footer
wwwroot/Static files: CSS, JavaScript, images
appsettings.jsonSettings such as connection strings and logging
Properties/launchSettings.jsonPort numbers and settings for local running
SchoolSite.csprojThe project file: .NET version and packages

Only files inside wwwroot can be downloaded directly by the browser. Your C# code and settings stay private on the server.

Program.cs

This file has two parts. First you register services (things your app needs). Then you build the app and add middleware (steps every request passes through).

Example

csharp
var builder = WebApplication.CreateBuilder(args);

// 1. Register services
builder.Services.AddRazorPages();

var app = builder.Build();

// 2. Configure the request pipeline (middleware)
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();

app.MapRazorPages();

app.Run();

What is Middleware?

Think of middleware as checkpoints on a road. Each request passes through them in order:

  1. UseHttpsRedirection sends http:// visitors to https://
  2. UseStaticFiles serves CSS, JS and images from wwwroot
  3. UseRouting finds which page or endpoint matches the URL
  4. UseAuthorization checks if the user is allowed in
  5. MapRazorPages runs the matching page

The order matters. For example, authorization must come after routing.

You can write your own simple middleware:

Example

csharp
app.Use(async (context, next) =>
{
    Console.WriteLine($"Request: {context.Request.Path}");
    await next();
});
Output
Request: /
Request: /Privacy

This prints each requested path to the terminal.

appsettings.json

Settings live in JSON so you can change them without changing code:

Example

json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    }
  },
  "AllowedHosts": "*",
  "SchoolName": "Workownix Institute"
}

Read a setting in Program.cs:

Example

csharp
var schoolName = builder.Configuration["SchoolName"];
Console.WriteLine(schoolName);
Output
Workownix Institute

There is also appsettings.Development.json, which overrides values only while you develop on your computer.

Environments

ASP.NET Core knows if it runs in Development, Staging or Production. In Development you see detailed error pages; in Production visitors see a friendly error page instead. The environment comes from the ASPNETCORE_ENVIRONMENT setting.

Exercise

Exercise

Which folder holds CSS files and images that the browser can download?

Learn ASP.NET with a teacher

Join the Full Stack Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp