# Project Structure and Running the App

Course: [Blazor](https://www.devtutorials.ai/learn/blazor) · Section: Foundations
Source: https://www.devtutorials.ai/learn/blazor/foundations/project-structure

> What every file and folder in a new Blazor Web App does: the .csproj, Program.cs, App.razor, _Imports.razor, wwwroot, the Components and Pages folders and launchSettings.json. Then how to run the app from the IDE or the CLI, and Hot Reload.

## Project Structure

- A **project** is the folder that holds all the files of one app.
- In **Getting Started**, you created a Blazor Web App called **BlazorApp**.
- In this lesson, you will see what each file and folder does, and how to run the app.

**Real-World Example:**

- Ravi created **BlazorApp** for his tea shop.
- Before he adds his menu, he wants to know where things go.
- Pages go in one folder, pictures in another, and settings in a third.
- Knowing this saves him time every day.

A new Blazor Web App has these files. Click each one to see its job.

**Where do I work most?** In `Components/Pages/`. Almost every new screen you build is a `.razor` file in that folder.

### Project File (.csproj)

- `BlazorApp.csproj` describes the project.
- `TargetFramework` says which .NET version it uses.
- Packages you add later are listed here, too.

      <TargetFramework>net10.0</TargetFramework>
      <Nullable>enable</Nullable>
      <ImplicitUsings>enable</ImplicitUsings>

`}
/>

### Startup File (Program.cs)

- `Program.cs` runs first when the app starts.
- It adds the services Blazor needs, then starts the web server.
- Below is a shortened copy. The real file also has error pages for production.

Watch the startup order. The arrow shows what runs first.

<figure className="blz-fig">
<svg viewBox="0 0 640 120" role="img" aria-labelledby="fig-blz-start">
  <title id="fig-blz-start">Animation: Program.cs starts the app, then App.razor, then Routes.razor, then the page that matches the address, such as Home.razor.</title>
  <defs><marker id="blz-a2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto"><path d="M0 0 L10 5 L0 10 z" style={{ fill: "var(--muted)" }} /></marker></defs>
  <rect className="grn" x="10" y="36" width="130" height="48" rx="10" />
  <text className="t m" x="75" y="65" textAnchor="middle" style={{ fontSize: 13, fontWeight: 700 }}>Program.cs</text>
  <rect className="pur" x="170" y="36" width="130" height="48" rx="10" />
  <text className="t m" x="235" y="65" textAnchor="middle" style={{ fontSize: 13, fontWeight: 700 }}>App.razor</text>
  <rect className="pur" x="330" y="36" width="130" height="48" rx="10" />
  <text className="t m" x="395" y="65" textAnchor="middle" style={{ fontSize: 13, fontWeight: 700 }}>Routes.razor</text>
  <rect className="blu" x="490" y="36" width="140" height="48" rx="10" />
  <text className="t m" x="560" y="58" textAnchor="middle" style={{ fontSize: 13, fontWeight: 700 }}>Home.razor</text>
  <text className="t" x="560" y="75" textAnchor="middle" style={{ fontSize: 11 }}>page for "/"</text>
  <path className="line" d="M140 60 H166" markerEnd="url(#blz-a2)" />
  <path className="line" d="M300 60 H326" markerEnd="url(#blz-a2)" />
  <path className="line" d="M460 60 H486" markerEnd="url(#blz-a2)" />
  <rect x="10" y="94" width="130" height="6" rx="3" style={{ fill: "#6d28d9" }}>
    <animate attributeName="x" values="10;170;330;490;490" dur="4s" repeatCount="indefinite" />
    <animate attributeName="width" values="130;130;130;140;140" dur="4s" repeatCount="indefinite" />
  </rect>
</svg>
<figcaption>Program.cs starts the server. App.razor draws the page frame. Routes.razor picks the page.</figcaption>
</figure>

### Root Component (App.razor)

- `Components/App.razor` is the **root component**: the first component Blazor draws.
- It holds the full HTML page: `html`, `head` and `body`.
- Inside the body, it shows `Routes`, which picks the page.
- `@Assets["app.css"]` finds the file in `wwwroot`. Below is a shortened copy.

<html lang="en">
<head>
      <meta charset="utf-8" />
      <base href="/" />
      <link rel="stylesheet" href="@Assets["app.css"]" />
</head>
<body>
      <script src="@Assets["_framework/blazor.web.js"]"></script>
</body>
</html>
`}
/>

### Global Imports (_Imports.razor)

First, two words you need:

- **Namespace** = a group name for C# classes, like a family name. For example, the `Tea` class below lives in the namespace `BlazorApp.Models`.
- **`@using`** = a line that says "I want to use the classes from this group". Without it, your page cannot find `Tea`.

Now the file:

- `_Imports.razor` is a list of `@using` lines.
- Every component in the same folder, and in the folders inside it, gets these lines for free.
- So you write a `@using` **once** here, not at the top of every page.

<figure className="blz-fig">
<svg viewBox="0 0 600 200" role="img" aria-labelledby="fig-blz-imports">
  <title id="fig-blz-imports">_Imports.razor holds one line, @using BlazorApp.Models. Arrows share it with Home.razor, Menu.razor and Order.razor, so each page can use the Tea class.</title>
  <defs><marker id="blz-a4" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto"><path d="M0 0 L10 5 L0 10 z" style={{ fill: "var(--muted)" }} /></marker></defs>
  <rect className="pur" x="20" y="64" width="220" height="72" rx="10" />
  <text className="t m" x="130" y="92" textAnchor="middle" style={{ fontSize: 13, fontWeight: 700 }}>_Imports.razor</text>
  <text className="t m" x="130" y="116" textAnchor="middle" style={{ fontSize: 12 }}>@using BlazorApp.Models</text>
  <rect className="blu" x="380" y="16" width="200" height="44" rx="8" />
  <text className="t m" x="480" y="43" textAnchor="middle" style={{ fontSize: 12 }}>Home.razor ✓ can use Tea</text>
  <rect className="blu" x="380" y="78" width="200" height="44" rx="8" />
  <text className="t m" x="480" y="105" textAnchor="middle" style={{ fontSize: 12 }}>Menu.razor ✓ can use Tea</text>
  <rect className="blu" x="380" y="140" width="200" height="44" rx="8" />
  <text className="t m" x="480" y="167" textAnchor="middle" style={{ fontSize: 12 }}>Order.razor ✓ can use Tea</text>
  <path className="line" d="M240 90 L376 40" markerEnd="url(#blz-a4)" />
  <path className="line" d="M240 100 L376 100" markerEnd="url(#blz-a4)" />
  <path className="line" d="M240 110 L376 160" markerEnd="url(#blz-a4)" />
</svg>
<figcaption>Write the @using line once in _Imports.razor. Every page in the folder gets it.</figcaption>
</figure>

In the box below:

- `Models/Tea.cs` makes the `Tea` class, in the namespace `BlazorApp.Models`.
- `_Imports.razor` has the line `@using BlazorApp.Models`.
- So `Home.razor` can use `Tea`, with no `@using` line of its own. Press **▶ Run**.

@foreach (var tea in teas)
{
      <p>@tea.Name costs ₹@tea.Price</p>
}

@code {
      private List<Tea> teas = new()
      {
          new Tea { Name = "Masala Tea", Price = 20 },
          new Tea { Name = "Ginger Tea", Price = 25 },
      };
}
`,
  }}
/>

- **Try it:** delete the line in `_Imports.razor`, then press **▶ Run**. You get an error, because `Home.razor` cannot find `Tea` any more. Press **Restore code** to fix it.

### Static Assets (wwwroot)

- `wwwroot/` holds files the browser downloads as they are: CSS, images, icons.
- `wwwroot/app.css` is available at `/app.css`.
- Put Ravi's logo at `wwwroot/images/logo.png`, and use it with `<img src="images/logo.png" />`.

### Components Folder

- `Components/` holds all your `.razor` files.
- `Components/Pages/` holds **pages**: components with a `@page` address, like `Home.razor` and `Menu.razor`.
- `Components/Layout/` holds the **layout**: `MainLayout.razor` (the frame of every page) and `NavMenu.razor` (the side menu).
- Small reusable pieces, like `MenuItem.razor`, can go straight in `Components/` or in a folder you create.

The box below has a page in `Components/Pages/` and a small reusable piece in `Components/Layout/`. The page uses it by its file name: `<ShopHeader />`.

<p>Today's special: Masala Tea</p>
`,
    "Components/Layout/ShopHeader.razor": `<h1>Ravi's Tea Shop</h1>
<p>Open 7 AM to 9 PM</p>
`,
  }}
/>

- `ShopHeader.razor` has no `@page`, so it is not a page. It is a piece that pages use.
- Change the opening hours in `ShopHeader.razor` and press **▶ Run**.

### Pages Folder

- Every file in `Components/Pages/` starts with `@page` and a web address.
- `@page "/"` is the home page. `@page "/menu"` opens at `/menu`.

In the below example, `Menu.razor` starts with `@page "/menu"`, so it is a page at `/menu`. It shows Ravi's menu as a table. Press **▶ Run**.

<table>
      <thead>
          <tr><th>Tea</th><th>Price</th></tr>
      </thead>
      <tbody>
          @foreach (var tea in teas)
          {
              <tr>
                  <td>@tea.Name</td>
                  <td>₹@tea.Price</td>
              </tr>
          }
      </tbody>
</table>

@code {
      private Tea[] teas =
      {
          new("Masala Tea", 20),
          new("Ginger Tea", 25),
          new("Lemon Tea", 15),
      };

      private record Tea(string Name, int Price);
}
`,
  }}
/>

- Add a new tea to the list, like `new("Mint Tea", 18)`, and press **▶ Run**.

### Launch Settings (launchSettings.json)

- `Properties/launchSettings.json` says how the app starts on **your computer**.
- It sets the web address (`applicationUrl`) and the environment (`Development`).
- It is not used when the app runs on a real server.

Your port numbers (like **7090**) will be different. The template picks random free ports when it creates the project.

**Simple lesson:** `Program.cs` starts the app, `App.razor` is the first component, and your work lives in `Components/`.

## Running the Application

### Running from IDE

- **Visual Studio:** press **F5** (run with the debugger) or **Ctrl+F5** (run without it). The app opens in your browser.
- **VS Code:** open `Program.cs`, then press **F5** and pick **C#**. Or click the **Run** button at the top right of the editor.
- Click **Counter** in the menu and press **Click me**. The number goes up.

### Running from CLI

- While the app runs, `Console.WriteLine` in your C# prints to that terminal. It is a quick way to see what your code is doing.
- In the code boxes on this site, it prints to the **Console** pane instead. Press **▶ Run**, then click the button.

<button @onclick="PlaceOrder">Order Masala Tea</button>
<p>@message</p>

@code {
      private string message = "";

      protected override void OnInitialized()
      {
          Console.WriteLine("Order page started.");
      }

      private void PlaceOrder()
      {
          message = "Thank you! Your tea is coming.";
          Console.WriteLine("Order placed: Masala Tea");
      }
}
`,
  }}
/>

### Hot Reload

- **Hot Reload** updates the running app when you save a file. You do not need to restart it.
- In Visual Studio, it is on by default. Save the file, or click the 🔥 **Hot Reload** button.
- From the CLI, use `dotnet watch` instead of `dotnet run`.

<figure className="blz-fig">
<svg viewBox="0 0 560 120" role="img" aria-labelledby="fig-blz-hot">
  <title id="fig-blz-hot">Animation: you save Home.razor, dotnet watch sends the change, and the open browser page updates without a restart.</title>
  <defs><marker id="blz-a3" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto"><path d="M0 0 L10 5 L0 10 z" style={{ fill: "var(--muted)" }} /></marker></defs>
  <rect className="pur" x="10" y="34" width="140" height="52" rx="10" />
  <text className="t" x="80" y="58" textAnchor="middle" style={{ fontSize: 13, fontWeight: 700 }}>Save Home.razor</text>
  <text className="t" x="80" y="76" textAnchor="middle" style={{ fontSize: 11 }}>Ctrl+S</text>
  <rect className="org" x="210" y="34" width="140" height="52" rx="10" />
  <text className="t" x="280" y="58" textAnchor="middle" style={{ fontSize: 13, fontWeight: 700 }}>dotnet watch 🔥</text>
  <text className="t" x="280" y="76" textAnchor="middle" style={{ fontSize: 11 }}>sends only the change</text>
  <rect className="blu" x="410" y="34" width="140" height="52" rx="10" />
  <text className="t" x="480" y="64" textAnchor="middle" style={{ fontSize: 13, fontWeight: 700 }}>
    <tspan>Page updates<animate attributeName="opacity" values="0.3;1;0.3" dur="2s" repeatCount="indefinite" /></tspan>
  </text>
  <path className="line" d="M150 60 H206" markerEnd="url(#blz-a3)" />
  <path className="line" d="M350 60 H406" markerEnd="url(#blz-a3)" />
  <circle r="5" cy="60" style={{ fill: "#ea580c" }}><animate attributeName="cx" values="150;206;350;406" keyTimes="0;0.3;0.7;1" dur="2s" repeatCount="indefinite" /></circle>
</svg>
<figcaption>Hot Reload sends your change into the running app. The page keeps its place.</figcaption>
</figure>

In the labs on this site, **▶ Run** plays the part of Hot Reload. Change the text below, then press **▶ Run** again.

<p>Cups ordered: @cups</p>
<button @onclick="OrderCup">Order a cup</button>

@code {
      private int cups = 0;

      private void OrderCup()
      {
          cups++;
          Console.WriteLine($"Cups ordered: {cups}");
      }
}
`,
  }}
/>

- `Console.WriteLine` prints to the **Console** pane, like `dotnet run` prints to the terminal.
- Change the heading text, then press **▶ Run** again to see the change.

**Hot Reload cannot apply every change.** Text, HTML and most method bodies update at once. Bigger changes, like a new class or a renamed method, need a restart. `dotnet watch` tells you when, and can restart for you.

**Simple lesson:** Run with **F5** or `dotnet run`. Use `dotnet watch` (or Visual Studio's Hot Reload) to see changes without a restart.

## Common mistakes

<div className="tscroll">

| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
| Running `dotnet run` outside the project folder | The terminal knows where the project is | `cd BlazorApp` first | "Couldn't find a project to run" |
| Putting images in `Components/` | Any folder can serve files | Put them in `wwwroot/` | The browser gets a 404 for the image |
| Writing the same `@using` in every page | Each page needs its own copy | Put it once in `_Imports.razor` | Repeated lines, easy to forget one |
| Editing `launchSettings.json` for the live server | It also sets the real server's address | It is only for your computer | The live site ignores your change |
| Expecting Hot Reload for every change | Every edit updates without a restart | Restart for big changes, like a new class | Old code keeps running |

</div>

## Check Yourself

## Exercises

## What to Remember

- `BlazorApp.csproj` sets the .NET version. `Program.cs` runs first and starts the app.
- `App.razor` is the root component. `Routes.razor` picks the page.
- `_Imports.razor` shares `@using` lines. `wwwroot/` holds static files.
- Pages live in `Components/Pages/`. The layout lives in `Components/Layout/`.
- `launchSettings.json` sets the address and port on your computer.
- Run with **F5** or `dotnet run`. Use `dotnet watch` for Hot Reload.

Next: **Razor Syntax**
