如何設定一部即裝即用的 .NET 開發機
Setting Up a .NET Developer Machine That Just Works
這篇入門教學説明如何設定 .NET 開發機,涵蓋安裝 SDK、以 global.json 固定每個儲存庫使用的 SDK 版本,以及準備本機 SQL Server 和 HTTPS 憑證。文中亦介紹建立及測試主控台應用程式和 Web API,並提供日常使用的 dotnet CLI 速查表。
Note: this article was drafted with AI assistance and reviewed by Softinbit Technology before publishing. It is part of the series Zero to Full-Stack .NET.
Install the SDK, a database, Git, and Node once, pin them per repository, and stop losing your first week to "it works on my machine".
Part 1 of the "Zero to Full-Stack .NET" series. Section: Getting Started.
TL;DR
- Install the .NET 10 SDK and pin each repository's SDK with
global.json, so a teammate's preview install never changes your build.- Run a throwaway SQL Server in Docker or LocalDB, and trust the local HTTPS certificate once with
dotnet dev-certs https --trust.- Finish with a console app, a Web API,
.httpsmoke tests, and a dotnet CLI cheat sheet you will use every day.Level: Beginner · Prerequisites: A Windows, macOS, or Linux machine where you can install software · Reading time: 10 min
It is your first Monday. You clone the project, open it, and press Build. The output says the project targets a .NET version you do not have. You install something, try again, and now the app starts but crashes because it cannot reach SQL Server. You fix that, open the browser, and get a red "Your connection is not private" warning on localhost.
None of this is a real bug. It is a machine that was never set up. The team has been working on their laptops for years and nobody remembers which tools they installed, so the onboarding document is three lines long and two of them are wrong.
This article is the setup I wish someone had handed me. It goes in the order you will hit the problems, and every step ends with a command you can run to prove it worked.
What you will be able to do after this
- Install the current .NET SDK and pin the version a repository uses with
global.json - Run SQL Server locally in Docker or as LocalDB and connect to it from a query tool
- Trust the local HTTPS certificate so
https://localhoststops warning - Create, run, and test a console app and a Web API from the command line
- Use a dotnet CLI cheat sheet instead of clicking through menus
Installing the SDK and pinning it per repository
Download the SDK (not just the runtime) for the current LTS release from the .NET download page on dot.net. LTS means long-term support: it is the version most teams target because it receives fixes for three years. .NET 8 and .NET 10 are both LTS releases; pick the one your team uses, or .NET 10 if you are starting fresh (.NET 8 support ends in November 2026).
Check what you have:
dotnet --list-sdks
# 8.0.xxx [C:\Program Files\dotnet\sdk]
# 10.0.xxx [C:\Program Files\dotnet\sdk]
dotnet --version
# prints the SDK that will be used in the current folder
Having several SDKs installed is normal. The surprise is which one dotnet build picks: without any configuration, it uses the newest one on the machine. That is fine until a teammate installs a preview and a build starts behaving differently. The fix is a global.json at the root of the repository:
{
"sdk": {
"version": "8.0.100",
"rollForward": "latestFeature"
}
}
rollForward: latestFeature means "use 8.0.100 or any newer 8.0.x feature band, but never jump to 9 or 10". You can generate the file instead of typing it:
dotnet new globaljson --sdk-version 8.0.100 --roll-forward latestFeature
Run dotnet --version inside the repo afterwards. If it prints an error saying the requested SDK was not found, you now know exactly what to install.
Choosing an editor
| Editor | Pick it when |
|---|---|
| Visual Studio Community (2026 for .NET 10; 2022 supports up to .NET 9) | You are on Windows, you want the strongest debugger, designers for Windows Forms, and SQL Server tooling built in. Free for individuals, open source, and small teams. |
| VS Code with the C# Dev Kit extension | You are on macOS or Linux, you also write Angular or Node, or you want a lighter editor. Install "C# Dev Kit"; it pulls in the C# extension and adds a solution explorer and a test runner. |
Both are fine. Many teams use Visual Studio for the API and VS Code for the frontend folder of the same repository. When you install Visual Studio, select the "ASP.NET and web development" workload and, if you will touch desktop tools, ".NET desktop development".
SQL Server for development
You need a SQL Server you can break without consequences. There are two good options.
Docker works on every OS and is the easiest to reset. Install Docker Desktop (on Windows, with the WSL 2 backend), then:
docker run -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=Dev_Passw0rd" -p 1433:1433 --name sqldev -d mcr.microsoft.com/mssql/server:2022-latest
# check it stayed up
docker ps
# if it is missing, read why
docker logs sqldev
The password for the sa (system administrator) account must meet SQL Server's complexity rules: at least eight characters, using three of the four groups uppercase, lowercase, digits, and symbols. If it does not, the container exits within seconds and docker ps shows nothing. docker logs tells you.
On Windows without Docker, install SQL Server Express, or use LocalDB, which comes with SQL Server Express and as an optional Visual Studio component. LocalDB is a tiny on-demand instance that starts when you connect:
sqllocaldb info
# MSSQLLocalDB
sqllocaldb start MSSQLLocalDB
Connection strings for the two setups:
Docker: Server=localhost,1433;Database=Store;User Id=sa;Password=Dev_Passw0rd;TrustServerCertificate=True
LocalDB: Server=(localdb)\MSSQLLocalDB;Database=Store;Trusted_Connection=True
TrustServerCertificate=True is needed for the container because recent SqlClient versions encrypt by default and the container's certificate is self-signed.
To look at tables and run queries, install SQL Server Management Studio (SSMS, Windows only) or, on any OS, VS Code with the MSSQL extension. Azure Data Studio was the older cross-platform option; Microsoft has retired it, so prefer the VS Code extension for new installs. Connect with the values above and run SELECT @@VERSION to confirm.
Git and a GitHub account
Install Git from git-scm.com (on Windows, keep the default "Git from the command line" option). Then tell Git who you are, because every commit is stamped with it:
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git --version
Create a GitHub account if you do not have one and set up authentication. The simplest path is the GitHub CLI: gh auth login walks you through a browser login and configures Git to use it. Your team may use Azure DevOps or GitLab instead; the Git commands are identical.
Node.js for the frontend you will write later
Angular, and most frontend tooling, run on Node.js. Do not install Node from the plain installer; use a version manager so you can switch when a project needs a different version. On Windows that is nvm-windows (the nvm-windows project by coreybutler on GitHub):
nvm install lts
nvm use lts
node --version
npm --version
On macOS or Linux use nvm from the nvm-sh project; there the commands are nvm install --lts and nvm use --lts. You do not need Angular today. When you do, npm install -g @angular/cli is one line.
Trusting the HTTPS development certificate
When you run a Web API, Kestrel (the built-in web server) serves https://localhost:xxxx with a self-signed certificate. Browsers reject it until you tell the operating system to trust it:
dotnet dev-certs https --trust
Windows and macOS show a confirmation dialog. On Linux, trust stores differ by distribution and browser, so depending on your SDK version the command may only print instructions; follow them. If something is broken, dotnet dev-certs https --clean followed by the trust command again fixes almost every case.
Your first console app
dotnet new console -n Store.Tools
cd Store.Tools
dotnet run
Open Program.cs. The template uses top-level statements: there is no class Program or Main, just code. Replace the contents with:
var customers = new[] { "Ayşe", "Mehmet", "Zeynep" };
foreach (var name in customers)
{
Console.WriteLine($"Merhaba, {name}");
}
dotnet run prints three greetings. If it does, the SDK, the compiler, and your terminal all work.
Your first Web API
dotnet new webapi -n Store.Api
cd Store.Api
dotnet run --launch-profile https
In .NET 8 and later this template creates a minimal API: endpoints are defined directly in Program.cs with app.MapGet(...). If your team uses controllers, add --use-controllers to the new command. .NET 9 replaced the Swagger UI package with built-in OpenAPI document generation, so do not be surprised when a newer template has no /swagger page.
Plain dotnet run uses the first profile in Properties/launchSettings.json, which in the template is http, so you would only get http://localhost:5xxx. With --launch-profile https the console prints both URLs, typically https://localhost:7xxx and http://localhost:5xxx. Add a small endpoint below the existing ones (above app.Run()):
app.MapGet("/products", () => new[]
{
new { Id = 1, Name = "Klavye", Price = 1299.90m },
new { Id = 2, Name = "Mouse", Price = 449.50m }
});
Then test it without a browser.
Testing HTTP requests from your editor
Visual Studio and VS Code (with the REST Client extension) both understand .http files, and the .NET 8 template already creates Store.Api.http next to the project. Replace its contents with this, using the HTTPS port dotnet run --launch-profile https printed:
@baseUrl = https://localhost:7001
GET {{baseUrl}}/products
Accept: application/json
###
GET {{baseUrl}}/weatherforecast
Click "Send Request" above a request and the response opens beside it. The file is plain text, so it goes into Git and the whole team shares the same smoke tests. Postman does the same job with a richer UI if you prefer it; export the collection into the repository so it is not trapped in one person's account.
The dotnet CLI cheat sheet
dotnet new list # see available templates
dotnet new sln -n Store # create a solution
dotnet sln add Store.Api/Store.Api.csproj # add a project to it
dotnet restore # download NuGet packages
dotnet build # compile
dotnet run --project Store.Api # run a project from the solution folder
dotnet watch --project Store.Api # rebuild and restart on every save
dotnet test # run all tests in the solution
dotnet add Store.Api package Serilog.AspNetCore # add a NuGet package to a project
dotnet tool install -g dotnet-ef # install a global tool
dotnet format # apply code style from .editorconfig
Commands run against the project in the current folder unless you pass --project. Keeping a terminal open next to the editor is faster than the menus once you know these.
The tools and what each one is for
| Tool | Purpose |
|---|---|
| .NET SDK | Compiler, runtime, and the dotnet CLI |
| Visual Studio or VS Code with C# Dev Kit | Editing and debugging |
| Docker Desktop with the SQL Server image, or LocalDB | A throwaway database |
| SSMS or VS Code with the MSSQL extension | Browsing tables and running T-SQL |
| Git and the GitHub CLI | Version control and pull requests |
| nvm-windows and Node.js LTS | Frontend tooling and the Angular CLI |
.http files or Postman |
Calling your API by hand |
dotnet-ef global tool |
EF Core migrations from the command line |
Common mistakes
- Several SDKs and no
global.json. The build silently picks the newest SDK and behaves differently on each machine. Addglobal.jsonand commit it. - Cloning into a OneDrive, Dropbox, or Google Drive folder. The sync client locks files in
binandobjmid-build and you get random "file in use" errors. Clone toC:\srcor~/src. - Running Visual Studio as administrator out of habit. Files and certificates created under admin become unreadable from a normal terminal. Run it as your own user; nothing in a normal web project needs elevation.
- Committing
binandobj. They are build output, change on every build, and cause merge conflicts. Rundotnet new gitignorein the repository root before the first commit. - Skipping
dotnet dev-certs https --trust. Every browser warning and everyHttpClientcall to your own API fails with a certificate error. One command fixes it. - Port 1433 already taken. A previously installed SQL Server Express is listening, so the Docker container cannot bind. Either stop the Windows service or map the container to another port with
-p 14333:1433and uselocalhost,14333in the connection string. - An
sapassword that fails the complexity rules. The container starts, fails validation, and exits.docker logs sqldevshows the message; pick a longer password with mixed character types.
Checklist
-
dotnet --list-sdksshows the SDK the team targets -
global.jsonexists in the repository root anddotnet --versionmatches it - SQL Server answers
SELECT @@VERSIONfrom SSMS or VS Code -
dotnet dev-certs https --trustran without errors -
git config --global user.nameanduser.emailare set -
node --versionprints an LTS version installed through nvm -
dotnet run --launch-profile httpsin a freshwebapiproject serves/weatherforecastover HTTPS -
.gitignorefromdotnet new gitignoreis committed beforebinandobjever are
What's next
With a machine that builds and a database that answers, the next article, "Thinking Like a Programmer: Turning a Vague Request Into Working Code", takes a one-sentence request from a product owner and turns it into a small, tested C# program. After that, "Git for Daily Work: Branches, Pull Requests, and Fixing Mistakes" covers the version control habits that keep a team from overwriting each other.
Originally published at www.softinbit.com. Part of Zero to Full-Stack .NET.
來源:DEV Community · dev.to