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.csprojdescribes the project.TargetFrameworksays which .NET version it uses.- Packages you add later are listed here, too.
Startup File (Program.cs)
Program.csruns 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.razoris the root component: the first component Blazor draws.- It holds the full HTML page:
html,headandbody. - Inside the body, it shows
Routes, which picks the page. @Assets["app.css"]finds the file inwwwroot. 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
Teaclass below lives in the namespaceBlazorApp.Models. @using= a line that says "I want to use the classes from this group". Without it, your page cannot findTea.
Now the file:
_Imports.razoris a list of@usinglines.- Every component in the same folder, and in the folders inside it, gets these lines for free.
- So you write a
@usingonce 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.csmakes theTeaclass, in the namespaceBlazorApp.Models._Imports.razorhas the line@using BlazorApp.Models.- So
Home.razorcan useTea, with no@usingline of its own. Press ▶ Run.
- Try it: delete the line in
_Imports.razor, then press ▶ Run. You get an error, becauseHome.razorcannot findTeaany more. Press Restore code to fix it.
Static Assets (wwwroot)
wwwroot/holds files the browser downloads as they are: CSS, images, icons.wwwroot/app.cssis 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.razorfiles.Components/Pages/holds pages: components with a@pageaddress, likeHome.razorandMenu.razor.Components/Layout/holds the layout:MainLayout.razor(the frame of every page) andNavMenu.razor(the side menu).- Small reusable pieces, like
MenuItem.razor, can go straight inComponents/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.razorhas no@page, so it is not a page. It is a piece that pages use.- Change the opening hours in
ShopHeader.razorand press ▶ Run.
Pages Folder
- Every file in
Components/Pages/starts with@pageand 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.jsonsays 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.WriteLinein 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 watchinstead ofdotnet 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.WriteLineprints to the Console pane, likedotnet runprints 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
| 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 |
Check Yourself
Exercises
What to Remember
BlazorApp.csprojsets the .NET version.Program.csruns first and starts the app.App.razoris the root component.Routes.razorpicks the page._Imports.razorshares@usinglines.wwwroot/holds static files.- Pages live in
Components/Pages/. The layout lives inComponents/Layout/. launchSettings.jsonsets the address and port on your computer.- Run with F5 or
dotnet run. Usedotnet watchfor Hot Reload.
Next: Razor Syntax