.NET + React · deploy guide
Host an ASP.NET Core API and a React frontend
Run an ASP.NET Core API with PostgreSQL and a React (Vite) frontend as two Bashrack hosting apps, built from your GitHub repository.
about 20 minutes of hands-on time
Who it’s for
Teams with an ASP.NET Core Web API and a React single-page app in GitHub.
You’ll need
- A GitHub repository (one for both projects or one each)
- ASP.NET Core 8 or newer
- A React app built with Vite (or another tool that outputs static files)
Steps
step 1
Dockerfile for the API
A multi-stage build: the SDK image compiles, the smaller runtime image runs. We set ASPNETCORE_HTTP_PORTS to the app's port, so the API listens where we send traffic. Replace MyApi with your project name.
dockerfileFROM mcr.microsoft.com/dotnet/sdk:9.0 AS build WORKDIR /src COPY *.csproj ./ RUN dotnet restore COPY . . RUN dotnet publish -c Release -o /app --no-restore FROM mcr.microsoft.com/dotnet/aspnet:9.0 WORKDIR /app COPY --from=build /app . EXPOSE 8080 ENTRYPOINT ["dotnet", "MyApi.dll"]step 2
Read the connection string
Name the database variable ConnectionStrings__Default. It arrives in the Npgsql format (Host=...;Username=...;Password=...;SSL Mode=Require), so GetConnectionString("Default") works unchanged. Environment variables override appsettings files; keep appsettings.Production.json valid JSON, or the app stops at startup.
csharpvar builder = WebApplication.CreateBuilder(args); builder.Services.AddDbContext<AppDb>(o => o.UseNpgsql(builder.Configuration.GetConnectionString("Default")));step 3
Dockerfile for the React frontend
Vite writes VITE_* values into the bundle at build time, so put the API address in .env.production in the repository (it is public anyway). nginx serves the files; the fallback sends every route to index.html.
dockerfileFROM node:22-alpine AS build WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build FROM nginx:1.27-alpine COPY --from=build /app/dist /usr/share/nginx/html RUN printf 'server {\n listen 80;\n root /usr/share/nginx/html;\n location / { try_files $uri /index.html; }\n}\n' > /etc/nginx/conf.d/default.conf EXPOSE 80step 4
Allow the frontend in CORS
The frontend and the API have different addresses, so the API must allow the frontend's origin.
csharpbuilder.Services.AddCors(o => o.AddDefaultPolicy(p => p.WithOrigins("https://my-frontend.bashrack.net").AllowAnyHeader().AllowAnyMethod())); // ... app.UseCors();step 5
Create the two apps
Create one app for the API (with the database ticked, variable ConnectionStrings__Default) and one for the frontend. If both live in one repository, set each app's Dockerfile path (for example api/Dockerfile and web/Dockerfile). Leave the port empty; it is taken from EXPOSE.
step 6
Connect the database
If the database was ordered separately, open it and choose Use in app: pick the app and the variable name. The app restarts with the variable set. Never commit the connection string to the repository.
Security and upkeep
- Keep secrets in the app's environment variables, never in appsettings files in the repository
- Allow only your frontend's origin in CORS
- Run database migrations deliberately (a one-off command or at startup with a lock), not on every request