TExcelTAD
Home › ASP.NET Core › Deploying ASP.NET Core

Deploying ASP.NET Core

An ASP.NET Core app is an ordinary .NET program with a built-in web server, Kestrel. Deploying it means three things: publish the compiled output, run it somewhere with the right configuration, and usually put a reverse proxy or load balancer in front of it.

Publishing

dotnet publish compiles the app and copies everything it needs into one folder. Since .NET 8 it builds in Release configuration by default.

bash · try it yourself
$ dotnet publish -o ./publish
$ ls publish | head -5
$ cd publish && ASPNETCORE_ENVIRONMENT=Production dotnet NotesApi.dll --urls http://0.0.0.0:5000
Restore complete (0.6s)
  NotesApi succeeded (3.4s) → publish/
Build succeeded in 4.5s
NotesApi
NotesApi.deps.json
NotesApi.dll
NotesApi.pdb
NotesApi.runtimeconfig.json
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://0.0.0.0:5000
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /srv/notes/publish
Publish style Command Server needs Notes
Framework-dependent dotnet publish the ASP.NET Core 9 runtime smallest output; the default
Self-contained dotnet publish -r linux-x64 --self-contained nothing bundles the runtime (~100 MB)
Single file add -p:PublishSingleFile=true depends on the above one executable
Native AOT <PublishAot>true</PublishAot> nothing fast start, small memory; minimal APIs only, limited reflection

Note: Before going live, make sure the environment is Production (the default when ASPNETCORE_ENVIRONMENT is unset), secrets come from environment variables or a vault — not appsettings.json — and UseHttpsRedirection/HSTS are configured for your setup.

Kestrel and URLs

Kestrel is a fast, cross-platform server that can face the internet directly. Choose where it listens with any of these (later entries win):

Setting Example
appsettings.json "Kestrel": { "Endpoints": { "Http": { "Url": "http://0.0.0.0:5000" } } }
Environment variable ASPNETCORE_URLS=http://+:5000 or ASPNETCORE_HTTP_PORTS=8080
Command line --urls http://0.0.0.0:5000
Code builder.WebHost.UseUrls(...) or app.Run("http://0.0.0.0:5000")

Kestrel settings such as request size limits live in code or configuration:

csharp
builder.WebHost.ConfigureKestrel(options =>
{
    options.Limits.MaxRequestBodySize = 10 * 1024 * 1024;   // 10 MB
    options.AddServerHeader = false;                          // hide "Server: Kestrel"
});

Containers

The official images on mcr.microsoft.com come in two flavours you'll use together: dotnet/sdk:9.0 to build, dotnet/aspnet:9.0 to run. A multi-stage Dockerfile keeps the SDK out of the final image:

dockerfile
# ---- build stage ----
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS build
WORKDIR /src
COPY NotesApi.csproj .
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o /app --no-restore

# ---- runtime stage ----
FROM mcr.microsoft.com/dotnet/aspnet:9.0
WORKDIR /app
COPY --from=build /app .
USER $APP_UID
EXPOSE 8080
ENTRYPOINT ["dotnet", "NotesApi.dll"]

Two .NET 8+ defaults matter here: the images listen on port 8080 (ASPNETCORE_HTTP_PORTS=8080) instead of 80, and they include a non-root app user, selected with USER $APP_UID.

bash · try it yourself
$ docker build -t notes-api .
$ docker run -d -p 5000:8080 --name notes notes-api
$ docker logs notes
$ curl -s localhost:5000/healthz
[+] Building 41.8s (14/14) FINISHED
 => exporting to image                                         0.4s
 => => naming to docker.io/library/notes-api                   0.0s
3c9f0e5d2a7b41e8c6f1d0a9b8e7c6d5f4a3b2c1d0e9f8a7b6c5d4e3f2a1b0c9
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://[::]:8080
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /app
Healthy
Image tag Contents Typical size
aspnet:9.0 Debian + ASP.NET Core runtime ~220 MB
aspnet:9.0-alpine Alpine Linux + runtime ~110 MB
aspnet:9.0-noble-chiseled Ubuntu "chiseled": no shell, no package manager, non-root ~120 MB
runtime-deps:9.0-noble-chiseled only native dependencies, for self-contained/AOT apps ~25 MB + your app

Tip: You can skip the Dockerfile entirely: dotnet publish /t:PublishContainer builds an OCI image with the SDK's built-in container support, choosing a matching base image for you.

Behind a reverse proxy

In production you often run Nginx, Apache, IIS, a cloud load balancer or a Kubernetes ingress in front of Kestrel. The proxy terminates HTTPS, serves as a single public entry point, and forwards requests to one or more app instances:

text
browser ──HTTPS──▶ Nginx :443 ──HTTP──▶ Kestrel :5000 (app instance 1)
                                  └──▶ Kestrel :5001 (app instance 2)
nginx
server {
    listen 443 ssl;
    server_name notes.example.com;

    location / {
        proxy_pass         http://127.0.0.1:5000;
        proxy_http_version 1.1;
        proxy_set_header   Host $host;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    }
}

The app now sees every request as plain HTTP from 127.0.0.1. The forwarded headers middleware restores the original client IP and scheme so that redirects, Request.IsHttps, rate limiting and logs are correct. Run it first:

csharp
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
    options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
    options.KnownProxies.Add(IPAddress.Parse("10.0.0.5"));   // only trust your proxy
});

var app = builder.Build();
app.UseForwardedHeaders();

Why "only trust your proxy"? Anyone can send an X-Forwarded-For header. The middleware walks the list from right to left and stops at the first address that is not a known proxy:

csharp · try it yourself
using System;
using System.Linq;

string[] knownProxies = { "10.0.0.5" };

string ClientIp(string remoteIp, string? xForwardedFor)
{
    if (!knownProxies.Contains(remoteIp) || xForwardedFor is null)
        return remoteIp;                            // untrusted sender: ignore the header
    var hops = xForwardedFor.Split(',').Select(h => h.Trim()).Reverse();
    foreach (var hop in hops)
        if (!knownProxies.Contains(hop)) return hop; // first non-proxy from the right
    return remoteIp;
}

Console.WriteLine(ClientIp("10.0.0.5", "203.0.113.7"));
Console.WriteLine(ClientIp("10.0.0.5", "1.2.3.4, 203.0.113.7"));
Console.WriteLine(ClientIp("198.51.100.9", "127.0.0.1"));
Console.WriteLine(ClientIp("10.0.0.5", null));
203.0.113.7
203.0.113.7
198.51.100.9
10.0.0.5

In the second line, a client tried to spoof 1.2.3.4, but only the right-most entry was added by the trusted proxy, so the real address wins. In the third, the header came straight from the internet and is ignored.

Hosting options at a glance

Where How the app runs
Linux VM systemd service running dotnet NotesApi.dll, Nginx in front
Windows / IIS ASP.NET Core Module hosts the app in-process in IIS
Containers Docker, Kubernetes, Azure Container Apps, AWS ECS
PaaS Azure App Service, AWS Elastic Beanstalk

Test yourself

On which port do the official .NET 8+ ASP.NET Core container images listen by default?

  1. 80
  2. 5000
  3. 8080
  4. 443

Why must UseForwardedHeaders() be configured with known proxies?

  1. Otherwise Kestrel refuses to start
  2. So that clients cannot spoof their IP or scheme with fake X-Forwarded-* headers
  3. It enables HTTP/3
  4. It is only needed for IIS

Which publish style needs no .NET runtime installed on the server?

  1. Framework-dependent
  2. dotnet run
  3. Debug build
  4. Self-contained