Publishing an ASP.NET Core application

Lesson 7 of 7 · ASP.NET Core

The shop (Prodavnica) works on your computer; the last step is to put it on the internet. We show the preparation (dotnet publish, settings for production) and two ways: Azure App Service (Microsoft's platform) and Render with Docker.

Before you start: required prior knowledge – MVC and Razor, publishing a static site.
How to learn: work in your own project step by step and run the application after every change; do the task at the end on your own first. The code is in C# for .NET 8 or newer (recommended: the current LTS version); the file name is in the comment at the top of every example.

Preparation

dotnet publish -c Release -o publish

The folder publish contains the compiled program (Prodavnica.dll) and everything it needs. It is started with dotnet Prodavnica.dll – that is how the server starts it too.

// Program.cs – after builder.Build(): apply the migrations at startup (creates the database on the server)
using (var scope = app.Services.CreateScope())
{
    var db = scope.ServiceProvider.GetRequiredService<ProdavnicaContext>();
    db.Database.Migrate();
}
On the serverHow
the Production environmentthe default when the app is not started from a development tool – detailed error pages are turned off
the database address and secretsenvironment variables: ConnectionStrings__Prodavnica replaces the value from appsettings.json (two underscores = a nested key)
HTTPSprovided by the platform; the application listens to HTTP on the port the platform gives it

Azure App Service

  1. Create an account on Azure for Students (students, without a card) or an ordinary free Azure account.
  2. In VS Code install the Azure App Service extension and sign in.
  3. Create New Web App → a name (e.g. prodavnica-ana) → the runtime .NET of the same version as the project → the pricing plan Free (F1).
  4. Right-click the publish folder → Deploy to Web App.
  5. The application is at https://prodavnica-ana.azurewebsites.net (Azure may add a suffix to the name).

The same from Visual Studio: right-click the project → Publish → Azure. An SQLite file on App Service works for a trial; for a serious application an Azure SQL database is used.

Render with Docker

Render has no built-in support for .NET, but it runs Docker containers – "boxes" with the program and everything it needs. Add to the root of the project:

# Dockerfile
# stage 1: compiling (an image with the SDK)
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
COPY . .
RUN dotnet publish -c Release -o /app

# stage 2: running (a smaller image, only the runtime)
FROM mcr.microsoft.com/dotnet/aspnet:10.0
WORKDIR /app
COPY --from=build /app .
ENV ASPNETCORE_HTTP_PORTS=8080
EXPOSE 8080
ENTRYPOINT ["dotnet", "Prodavnica.dll"]

The number 10.0 must match the .NET version in the .csproj file (for .NET 8: 8.0). Also add .dockerignore with the lines bin/, obj/, publish/, *.db.

  1. Put the project on GitHub.
  2. On render.com: New → Web Service → the repository → Render recognises the Dockerfile → Free.
  3. After compiling, the application is at https://prodavnica.onrender.com.
Attention: the disk of a free service is erased at every restart, so an SQLite database in the container loses its data. For permanent data a database is used as a separate service (e.g. PostgreSQL on Render with the package Npgsql.EntityFrameworkCore.PostgreSQL and UseNpgsql). The terms of free plans change – check them before you start.

The most common problems

SymptomCause and solution
HTTP Error 500.30 / the application does not startlook at the logs (Log stream / Logs); often an environment variable or the database is missing
no such tablethe migrations were not applied – the code with Database.Migrate() from the preparation
CSS and images are not shownUseStaticFiles() is missing or the files are not in wwwroot
Docker: could not find Prodavnica.dllthe name in ENTRYPOINT must match the name of the project
the JavaScript client gets no datathe API address in the client still points to localhost – use the relative address /api/proizvodi

Practice tasks

Each task has an example, a hint and a solution.

TASK 01Difficulty: easy

Checking that the server works

Platforms check whether an application works by calling some address from time to time. Add the route /health that returns {"status":"ok","time":"…"} and the number of products in the database, and enter it as the Health Check Path on Render.

Common mistakes and tips
  • Before publishing check: dotnet build -c Release without errors and that there are no passwords in the code.
  • The .NET version on the platform (or in the Docker image) must be the same as in the project.
  • Do not publish appsettings.Development.json with personal settings to a public repository.

Check your knowledge

Open the quiz

Publishing

1. What does the command dotnet publish -c Release make?




2. How is the database address set on the server without changing the code?



3. Why does an SQLite database in a free Docker service lose its data?