Environment Setup & Your First App
6 min read
Environment Setup & Your First App
Welcome back, code traveler! 🎒 Now that you understand what Blazor is, it is time to get our hands dirty. We are moving from theory to the "I actually made something appear on the screen" phase.
This guide is designed to take you from a computer that knows nothing about C# to a running web application. We will follow industry-standard practices, skipping the fluff and focusing on the tools you will actually use in a paid job.
1. Installing the .NET SDK and IDE Setup 🛠️
The Problem
Your computer, by default, speaks binary and maybe a little OS-specific language. It does not understand C# or Blazor. If you type dotnet run into your terminal right now, your computer will likely look at you with confusion.
The Solution: The .NET SDK
To fix this, we need the .NET SDK (Software Development Kit).
Definition: The SDK is a collection of tools, libraries, and runtime components that allow developers to create, build, and run applications. It includes the CLI (Command Line Interface) and the Runtime (the thing that actually runs the code).
Step 1: Install the .NET SDK
We want the latest Long Term Support (LTS) or stable version. As of the modern .NET era (NET 8/9+), the installation is unified.
- Download: Go to the official Microsoft .NET download page.
- Select: Choose the .NET SDK (not just the Runtime) for your operating system (Windows, macOS, or Linux).
- Install: Run the installer. It’s a standard "Next, Next, Finish" affair. It’s surprisingly fast—unlike downloading
node_modules, which has been jokingly rumored to contain black holes. 🌌 - Verify: Open your terminal (Command Prompt, PowerShell, or Bash) and type:
dotnet --versionIf it prints a number (like 8.0.x or 9.0.x), you are golden.
Step 2: The IDE (Integrated Development Environment)
You could write code in Notepad, but you also could cut your lawn with scissors. Let's use proper tools.
Option A: Visual Studio Code (VS Code) - Recommended for this guide
This is the lightweight, cross-platform industry standard for modern web dev.
- Download VS Code.
- Install the C# Dev Kit extension by Microsoft. This enables "IntelliSense" (where the computer guesses what you want to type and is usually right).
Option B: Visual Studio (Community/Pro/Enterprise)
The heavy hitter. If you are on Windows and want the full enterprise experience, this is the tool.
- Download the Visual Studio Installer.
- Select the workload: "ASP.NET and web development".
Industry Tip: VS Code is preferred for frontend-heavy or cross-platform teams. Visual Studio is preferred for heavy backend/enterprise .NET shops.
2. Creating Your First Blazor Project: 'Hello World' 🌍
The Problem
Starting a project by creating empty text files and trying to link them together manually is a recipe for disaster. You need a structure—a skeleton—that follows best practices immediately.
The Solution: CLI Templates
The .NET CLI provides "templates." These are pre-configured project scaffolds that set up all the boring configuration files for you so you can start coding logic immediately.
Executing the Command
We will use the Blazor Web App template. This is the modern standard (introduced in .NET 8) that unifies previous hosting models.
- Create a folder for your projects (e.g.,
C:\Codeor~/Code). - Open your terminal in that folder.
- Run the following command:
dotnet new blazor -o MyFirstBlazorAppBreakdown of the command:
dotnet: Hey SDK, wake up.new: I want to create something.blazor: Use the "Blazor Web App" template.-o MyFirstBlazorApp: Create an Output directory namedMyFirstBlazorAppand put the files there.
Fun Fact: The first "Hello World" program was documented in 1972 by Brian Kernighan. We are keeping a 50+ year tradition alive!
3. Anatomy of a Blazor App 🩻
The Problem
You just created the app, and now there are files everywhere. Program.cs, App.razor, bin, obj. It looks intimidating. If you delete the wrong one, the app explodes.
The Solution: Understanding the Tree
Let's dissect the essential parts. We will ignore the noise (bin and obj folders are just temporary build artifacts—ignore them).
📂 The Project Structure
1. Program.cs (The Brain)
This is the entry point. Every .NET app starts here.
- What it does: It sets up the web server, configures "Services" (Dependency Injection), and tells the app how to handle HTTP requests.
- Code Snapshot:csharp
var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorComponents(); var app = builder.Build(); app.Run();
2. App.razor (The Root Component)
This is the root UI component.
- What it does: It sets up the basic HTML shell (
<html>,<body>) and loads the Router. - Concept - The Router: The Router looks at the browser URL (e.g.,
/counter) and decides which page to show you.
3. _Imports.razor (The VIP Pass)
- What it does: Instead of typing
using System.Net.Http;in every single file, you put it here once, and it applies to all files in the folder. It keeps your code clean.
4. Components/Pages/ (The Content)
This is where your actual screens live.
Home.razor: The landing page.Counter.razor: A classic interactive example.
5. wwwroot (The Public Folder)
- What it does: This is the only folder directly accessible by the browser. CSS files, images, icons, and JavaScript libraries live here.
4. Running, Building, and Hot Reloading 🏃♂️
The Problem
You write code. You save. You stop the server. You restart the server. You refresh the browser. You wait. You realize you made a typo. You repeat. This cycle kills productivity.
The Solution: dotnet watch (Hot Reload)
Definition: Hot Reload is a technology that injects code changes into the running application without losing the app's state.
How to Run Your App
Navigate into your new folder:
cd MyFirstBlazorAppOption 1: The "I'm Developing" Mode (Use this 99% of the time)
dotnet watch- What happens: It compiles the code, starts a local web server (usually at
localhost:5000or similar), and opens your browser. - The Magic: Change the text in
Home.razorand save the file. Look at your browser. It updated automatically without a restart.
Option 2: The "Just Run It" Mode
dotnet run- Use this if you don't need auto-updates.
Option 3: The "Ship It" Mode
dotnet publish -c Release- What happens: This packs your app into a tight, optimized bundle ready to be uploaded to a server (Azure, AWS, etc.). It trims out the unused code to make the file size smaller.
🧪 Practical Exercise: Modify the App
- Run
dotnet watch. - Open
Components/Pages/Home.razorin your IDE. - Change the
<h1>Hello, world!</h1>to<h1>Hello, FreeCodeCamp! ⛺</h1>. - Hit Save.
- Watch the browser update instantly.
Congratulations! You are officially a Blazor developer.
📚 References & Further Reading
Challenge
FizzBuzz Implementation
Your task is to write a function named fizzBuzz that takes a single integer argument, n.
The function must iterate from 1 up to and including n. For each number in this range, you must print a specific value based on the following rules:
- If the number is divisible by both 3 and 5, print the string "FizzBuzz".
- If the number is divisible by 3 (but not 5), print the string "Fizz".
- If the number is divisible by 5 (but not 3), print the string "Buzz".
- If the number is not divisible by either 3 or 5, print the number itself.
Each output should be on a new line.
Expected result
When the function fizzBuzz is called with the argument 15, the output should be a sequence of values printed to the console, each on a new line, exactly as follows:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz