Project Structure and Running the App

By Syncfusion®, Inc. · Last updated

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.

What you will learn

  • Name the job of the .csproj, Program.cs, App.razor and _Imports.razor
  • Know where pages, reusable components and static files go
  • Read launchSettings.json and find the app's web address
  • Run the app from the IDE or the CLI, and use 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.

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.

Animation: Program.cs starts the app, then App.razor, then Routes.razor, then the page that matches the address, such as Home.razor.

Program.cs

App.razor

Routes.razor

Home.razor page for "/"

Program.cs starts the server. App.razor draws the page frame. Routes.razor picks the page.

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.

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.

_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.

_Imports.razor @using BlazorApp.Models

Home.razor ✓ can use Tea

Menu.razor ✓ can use Tea

Order.razor ✓ can use Tea

Write the @using line once in _Imports.razor. Every page in the folder gets it.

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.
  • 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 />.

  • 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.

  • 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

Click Next to see each command. Your ports and times will be different.

  • 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.

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.

Output is shortened.

Animation: you save Home.razor, dotnet watch sends the change, and the open browser page updates without a restart.

Save Home.razor Ctrl+S

dotnet watch 🔥 sends only the change

Page updates

Hot Reload sends your change into the running app. The page keeps its place.

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

  • 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

MistakeWrong ideaCorrect ideaWhat goes wrong
Running dotnet run outside the project folderThe terminal knows where the project iscd BlazorApp first"Couldn't find a project to run"
Putting images in Components/Any folder can serve filesPut them in wwwroot/The browser gets a 404 for the image
Writing the same @using in every pageEach page needs its own copyPut it once in _Imports.razorRepeated lines, easy to forget one
Editing launchSettings.json for the live serverIt also sets the real server's addressIt is only for your computerThe live site ignores your change
Expecting Hot Reload for every changeEvery edit updates without a restartRestart for big changes, like a new classOld code keeps running

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