How do you build and run a .NET application?

BeginnerHands-on.NET builds (dotnet CLI)
Answer

Short answer

Use the dotnet CLI: dotnet restore downloads NuGet packages, dotnet build compiles, dotnet test runs tests, dotnet publish creates the files to deploy, and you run the result with dotnet MyApp.dll.

What the files mean

File Purpose
MyApp.csproj The project file — target framework and NuGet package references (like pom.xml)
MyApp.sln A solution file that groups several projects together
appsettings.json Application settings; appsettings.Production.json overrides them in production

Step by step

dotnet --version                               # e.g. 8.0.x

dotnet restore                                 # download NuGet packages
dotnet build -c Release                        # compile in Release mode
dotnet test                                    # run unit tests
dotnet publish -c Release -o ./publish         # everything needed to run, in ./publish

dotnet ./publish/MyApp.dll                     # start the app
# Now listening on: http://localhost:5000

Choose the environment and port:

export ASPNETCORE_ENVIRONMENT=Production       # loads appsettings.Production.json
export ASPNETCORE_URLS=http://0.0.0.0:8080
dotnet ./publish/MyApp.dll

Example — Dockerfile

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
COPY *.csproj .
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o /app

FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app .
EXPOSE 8080
ENTRYPOINT ["dotnet", "MyApp.dll"]

What to say in the interview

"With .NET we use the dotnet CLI: restore pulls NuGet packages, build compiles, test runs tests, and publish produces the deployable output, which we run with dotnet MyApp.dll. The environment is chosen with ASPNETCORE_ENVIRONMENT, which loads the matching appsettings file. In Docker we build with the SDK image and run on the smaller ASP.NET runtime image."

Follow-up questions you may get

  • What is NuGet? The package manager for .NET, like npm for Node.js or Maven Central for Java.
  • Framework-dependent vs self-contained publish? Framework-dependent needs .NET installed on the server; self-contained bundles the runtime too, so it's bigger but runs anywhere.
All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: .NET apps: dotnet build & publish

More on .NET builds (dotnet CLI)